Skip to content

Commit

Permalink
feat(cli): add "gas auth" command to authenticate TensorBay account
Browse files Browse the repository at this point in the history
PR Closed: #672
  • Loading branch information
Lee-000 committed Jun 7, 2021
1 parent bd7c2f1 commit cf6cd59
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
54 changes: 54 additions & 0 deletions tensorbay/cli/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env python3
#
# Copyright 2021 Graviti. Licensed under MIT License.
#

"""Implementation of gas auth."""


from configparser import ConfigParser
from typing import Dict

from .utility import error, form_profile_value, read_config, update_config, write_config


def _implement_auth(obj: Dict[str, str], arg1: str, arg2: str) -> None:
update_config()
config_parser = read_config()

if not arg1 and not arg2:
error("Require accessKey to authenticate Tensorbay account")

if _is_accesskey(arg1):
if _is_url(arg2):
error('Please use "gas auth [url] [accessKey]" to specify the url and accessKey')
if arg2:
error(f'Redundant argument "{arg2}"')

elif _is_url(arg1):
if not arg2:
error("Require accessKey to authenticate Tensorbay account")
if not _is_accesskey(arg2):
error("Wrong accesskey format")
else:
error(f'Invalid argument "{arg1}"')

_update_profile(config_parser, obj["profile_name"], arg1, arg2)
write_config(config_parser)


def _is_accesskey(arg: str) -> bool:
return arg.startswith(("Accesskey-", "ACCESSKEY-")) and len(arg) == 42


def _is_url(arg: str) -> bool:
return arg.startswith("https://")


def _update_profile(config_parser: ConfigParser, profile_name: str, arg1: str, arg2: str) -> None:
if not config_parser.has_section("profiles"):
config_parser.add_section("profiles")

config_parser["profiles"][profile_name] = (
form_profile_value(arg1) if not arg2 else form_profile_value(arg2, arg1)
)
18 changes: 18 additions & 0 deletions tensorbay/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,5 +387,23 @@ def log(
_implement_log(obj, tbrn, max_count, oneline)


@cli.command()
@click.argument("arg1", type=str, default="", metavar="accessKey")
@click.argument("arg2", type=str, default="", metavar="")
@click.pass_obj
def auth(obj: Dict[str, str], arg1: str, arg2: str) -> None:
"""Authenticate the accessKey of gas.\f
Arguments:
obj: A dict contains config information.
arg1: The accessKey or the url of gas for the authentication.
arg2: The accessKey of gas for the authentication if arg1 is url.
""" # noqa: D301,D415
from .auth import _implement_auth

_implement_auth(obj, arg1, arg2)


if __name__ == "__main__":
cli() # pylint: disable=no-value-for-parameter

0 comments on commit cf6cd59

Please sign in to comment.