Skip to content

Commit

Permalink
feat(cli): catch the known custom exception and print error message
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee-000 committed Aug 12, 2021
1 parent edea908 commit da39fb7
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 46 deletions.
50 changes: 33 additions & 17 deletions tensorbay/cli/branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,49 @@
import click

from ..client.gas import DatasetClientType
from ..exception import (
AccessDeniedError,
NameConflictError,
ResourceNotExistError,
StatusError,
TBRNError,
UnauthorizedError,
)
from .tbrn import TBRN, TBRNType
from .utility import ContextInfo, error, get_dataset_client, get_gas, shorten


def _implement_branch(
obj: ContextInfo, tbrn: str, name: str, verbose: bool, is_delete: bool
) -> None:
info = TBRN(tbrn=tbrn)
if info.type != TBRNType.DATASET:
error(f'To operate a branch, "{info}" must be a dataset')

gas = get_gas(*obj)
dataset_client = get_dataset_client(gas, info)

if is_delete:
_delete_branch(dataset_client, info)
return

if name:
_create_branch(dataset_client, name)
else:
_list_branches(dataset_client, verbose)
try:
info = TBRN(tbrn=tbrn)
if info.type != TBRNType.DATASET:
error(f'To operate a branch, "{info}" must be a dataset')

gas = get_gas(*obj)
dataset_client = get_dataset_client(gas, info)

if is_delete:
_delete_branch(dataset_client, info)
return

if name:
_create_branch(dataset_client, name)
else:
_list_branches(dataset_client, verbose)

except (TBRNError, NameConflictError, ResourceNotExistError, UnauthorizedError) as err:
error(str(err))
except StatusError:
error("Branch cannot be created from a draft")
except AccessDeniedError:
error("You do not have permission to operate this dataset")


def _create_branch(dataset_client: DatasetClientType, name: str) -> None:
if dataset_client.status.is_draft:
error("Branch cannot be created from a draft")
# if dataset_client.status.is_draft:
# error("Branch cannot be created from a draft")

if not dataset_client.status.commit_id:
error(f'To create a branch, "{dataset_client.name}" must have commit history')
Expand Down
63 changes: 34 additions & 29 deletions tensorbay/cli/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,42 @@

import click

from ..exception import TensorBayException
from .tbrn import TBRN, TBRNType
from .utility import ContextInfo, error, get_gas


def _implement_dataset(obj: ContextInfo, tbrn: str, is_delete: bool, yes: bool) -> None:
gas = get_gas(*obj)
if is_delete:
if not tbrn:
error("Missing argument TBRN")

info = TBRN(tbrn=tbrn)
if info.type != TBRNType.DATASET:
error(f'"{tbrn}" is not a dataset')

if not yes:
click.confirm(
f'Dataset "{tbrn}" will be completely deleted.\nDo you want to continue?',
abort=True,
)

gas.delete_dataset(info.dataset_name)
click.echo(f'Dataset "{tbrn}" is deleted successfully')
return

if tbrn:
info = TBRN(tbrn=tbrn)
if info.type != TBRNType.DATASET:
error(f'"{tbrn}" is not a dataset')

gas.create_dataset(info.dataset_name)
click.echo(f'Dataset "{tbrn}" is created successfully')
else:
for dataset_name in gas.list_dataset_names():
click.echo(TBRN(dataset_name).get_tbrn())
try:
gas = get_gas(*obj)
if is_delete:
if not tbrn:
error("Missing argument TBRN")

info = TBRN(tbrn=tbrn)
if info.type != TBRNType.DATASET:
error(f'"{tbrn}" is not a dataset')

if not yes:
click.confirm(
f'Dataset "{tbrn}" will be completely deleted.\nDo you want to continue?',
abort=True,
)

gas.delete_dataset(info.dataset_name)
click.echo(f'Dataset "{tbrn}" is deleted successfully')
return

if tbrn:
info = TBRN(tbrn=tbrn)
if info.type != TBRNType.DATASET:
error(f'"{tbrn}" is not a dataset')

gas.create_dataset(info.dataset_name)
click.echo(f'Dataset "{tbrn}" is created successfully')
else:
for dataset_name in gas.list_dataset_names():
click.echo(TBRN(dataset_name).get_tbrn())

except TensorBayException as err:
error(str(err))
3 changes: 3 additions & 0 deletions tensorbay/cli/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from ..client import config as client_config
from ..client.dataset import DatasetClient, FusionDatasetClient
from ..client.gas import DatasetClientType
from ..client.requests import logger
from .tbrn import TBRN


Expand All @@ -39,6 +40,8 @@ def _implement_cli(

if debug:
logging.basicConfig(level=logging.DEBUG)
else:
logger.disabled = True


def _get_config_filepath() -> str:
Expand Down

0 comments on commit da39fb7

Please sign in to comment.