Skip to content

Commit

Permalink
Add debug and version flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Volara committed Aug 30, 2024
1 parent 7d7bc13 commit 1bd6574
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,9 @@ volara auth drive logout
```shell
volara update
```

#### Check the Volara CLI version

```shell
volara --version
```
11 changes: 10 additions & 1 deletion cli/auth/drive/_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,19 +14,22 @@

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(
tzinfo=None
)
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


Expand Down Expand Up @@ -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)
Expand All @@ -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)
24 changes: 24 additions & 0 deletions cli/debug.py
Original file line number Diff line number Diff line change
@@ -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)
16 changes: 11 additions & 5 deletions cli/entry.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand All @@ -29,7 +35,7 @@ def auth():
drive_entry.register(auth)


@volara.group()
@volara.group(cls=DebugCommandGroup)
def mine():
"""Commands related to mining"""
pass
Expand Down Expand Up @@ -97,4 +103,4 @@ def rewards():


if __name__ == "__main__":
volara()
volara(obj={})
2 changes: 2 additions & 0 deletions constants/__init__.py
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit 1bd6574

Please sign in to comment.