diff --git a/bananas_cli/cli.py b/bananas_cli/cli.py index 59da5e7..d66ca41 100644 --- a/bananas_cli/cli.py +++ b/bananas_cli/cli.py @@ -1,9 +1,11 @@ import click import logging +from aiohttp.client_exceptions import ClientConnectorError from .authentication import authenticate from .helpers import task from .session import Session +from .exceptions import Exit log = logging.getLogger(__name__) pass_session = click.make_pass_decorator(Session) @@ -19,9 +21,10 @@ @click.option("--tus-url", help="BaNaNaS tus URL. (normally the same as --api-url)", metavar="URL") @click.option("--client-id", help="Client-id to use for authentication", default="ape", show_default=True) @click.option("--audience", help="Audience to use for authentication", default="github", show_default=False) +@click.option("--verbose", help="Enable verbose output for errors, showing tracebacks", is_flag=True) @click.pass_context @task -async def cli(ctx, api_url, tus_url, client_id, audience): +async def cli(ctx, api_url, tus_url, client_id, audience, verbose): """ A CLI tool to list, upload, and otherwise modify BaNaNaS content. @@ -40,7 +43,7 @@ async def cli(ctx, api_url, tus_url, client_id, audience): if not tus_url: tus_url = api_url - session = Session(api_url, tus_url) + session = Session(api_url, tus_url, verbose) ctx.obj = session await session.start() @@ -49,7 +52,14 @@ async def cli(ctx, api_url, tus_url, client_id, audience): if "-h" in os_args or "--help" in os_args: return - await authenticate(session, client_id, audience) + try: + await authenticate(session, client_id, audience) + except (ClientConnectorError, NameError) as e: + if verbose: + log.exception(e) + else: + log.error(e) + raise Exit @task diff --git a/bananas_cli/commands/upload.py b/bananas_cli/commands/upload.py index ba5c292..95a76a6 100644 --- a/bananas_cli/commands/upload.py +++ b/bananas_cli/commands/upload.py @@ -28,6 +28,9 @@ def show_validation_errors(data): @pass_session @task async def upload(session, version, name, description, url, license, files): + if len(files) == 0: + log.error("No files specified for upload") + return parts = files[0].split("/")[:-1] for filename in files: check_parts = filename.split("/") diff --git a/bananas_cli/session.py b/bananas_cli/session.py index f9a4ed2..10be440 100644 --- a/bananas_cli/session.py +++ b/bananas_cli/session.py @@ -13,12 +13,13 @@ class Session: - def __init__(self, api_url, tus_url): + def __init__(self, api_url, tus_url, verbose): self.session = None self.api_url = f"{api_url}/" self.tus_url = f"{tus_url}/" self._headers = {} + self.verbose = verbose async def start(self): self.session = aiohttp.ClientSession() @@ -64,8 +65,11 @@ def tus_upload(self, upload_token, fullpath, filename): metadata={"filename": filename, "upload-token": upload_token}, ) uploader.upload() - except TusCommunicationError: - log.exception(f"Failed to upload file '{filename}'") + except TusCommunicationError as e: + if self.verbose: + log.exception(f"Failed to upload file '{filename}'") + else: + log.error(f"Failed to upload file '{filename}': {e}") raise Exit def set_header(self, header, value):