From 1bd65746d1756a591a9a1a1988fd521e9dbb890f Mon Sep 17 00:00:00 2001 From: Volara Date: Fri, 30 Aug 2024 19:22:28 -0400 Subject: [PATCH] Add debug and version flags --- README.md | 6 ++++++ cli/auth/drive/_impl.py | 11 ++++++++++- cli/debug.py | 24 ++++++++++++++++++++++++ cli/entry.py | 16 +++++++++++----- constants/__init__.py | 2 ++ 5 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 cli/debug.py diff --git a/README.md b/README.md index 3ed571a..f8d8a6b 100644 --- a/README.md +++ b/README.md @@ -116,3 +116,9 @@ volara auth drive logout ```shell volara update ``` + +#### Check the Volara CLI version + +```shell +volara --version +``` diff --git a/cli/auth/drive/_impl.py b/cli/auth/drive/_impl.py index 84e4015..40adcef 100644 --- a/cli/auth/drive/_impl.py +++ b/cli/auth/drive/_impl.py @@ -4,7 +4,7 @@ import requests import json import datetime as dt - +import logging from google.oauth2.credentials import Credentials from constants import TMP_DRIVE_AUTH, VOLARA_API @@ -14,6 +14,7 @@ def get_active_account() -> T.Optional[Credentials]: if os.path.exists(TMP_DRIVE_AUTH): + logging.debug("Found existing drive auth token.") with open(TMP_DRIVE_AUTH, "r") as token: code = json.load(token) code["expiry"] = dt.datetime.fromisoformat(code["expiry"][:-1]).replace( @@ -21,12 +22,14 @@ def get_active_account() -> T.Optional[Credentials]: ) creds = Credentials(**code) if creds.expired: + logging.debug("Drive auth token expired. Refreshing...") creds = _call_volara_api_server_refresh(creds) if creds is not None: _persist_credentials(creds) return creds if creds.valid: return creds + logging.debug("No active drive account found.") return None @@ -82,6 +85,7 @@ def _call_volara_api_server_refresh(creds: Credentials) -> T.Optional[Credential }, ) if url_response.status_code != 200: + logging.error(f"Failed to refresh drive token: {url_response.json()}") return resp = url_response.json()["tokens"] return _form_credentials_from_token(resp) @@ -96,3 +100,8 @@ def _form_credentials_from_token(resp: T.Dict[str, T.Any]) -> Credentials: ), } return Credentials(**code) + + +if __name__ == "__main__": + creds = get_active_account() + print(creds) diff --git a/cli/debug.py b/cli/debug.py new file mode 100644 index 0000000..bdd0247 --- /dev/null +++ b/cli/debug.py @@ -0,0 +1,24 @@ +import logging +import click + + +def set_debug_level(ctx, param, value): + if value: + click.echo("Debug logging enabled.") + logging.basicConfig(level=logging.DEBUG) + return value + + +class DebugCommandGroup(click.Group): + def add_command(self, cmd, name=None): + cmd.params.insert( + 0, + click.Option( + ("--debug", "-d"), + is_flag=True, + callback=set_debug_level, + expose_value=False, + help="Enable debug logging", + ), + ) + super().add_command(cmd, name) diff --git a/cli/entry.py b/cli/entry.py index 4e8edb7..83dedfc 100644 --- a/cli/entry.py +++ b/cli/entry.py @@ -1,5 +1,9 @@ import click +from constants import VERSION + +from cli.debug import DebugCommandGroup + import cli.miner as mining import cli.twitter_entry as twitter_entry import cli.drive_entry as drive_entry @@ -13,10 +17,12 @@ import cli.update as volara_update -@click.group() -def volara(): +@click.group(cls=DebugCommandGroup) +@click.version_option(version=VERSION) +@click.pass_context +def volara(ctx): """Volora CLI tool""" - pass + ctx.ensure_object(dict) @volara.group() @@ -29,7 +35,7 @@ def auth(): drive_entry.register(auth) -@volara.group() +@volara.group(cls=DebugCommandGroup) def mine(): """Commands related to mining""" pass @@ -97,4 +103,4 @@ def rewards(): if __name__ == "__main__": - volara() + volara(obj={}) diff --git a/constants/__init__.py b/constants/__init__.py index fc2e8a4..9cc2e96 100644 --- a/constants/__init__.py +++ b/constants/__init__.py @@ -1,5 +1,7 @@ import os +VERSION = "0.1.0" + VOLARA_TMP_DIR = os.path.expanduser("~/.volara") TMP_MINER_LOG = f"{VOLARA_TMP_DIR}/miner.log"