Skip to content

Commit

Permalink
fix: logout on success prints login
Browse files Browse the repository at this point in the history
  • Loading branch information
spencerwooo committed Jan 24, 2023
1 parent a4f0034 commit 5761a86
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
28 changes: 16 additions & 12 deletions bitsrun/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,34 +72,38 @@ def do_action(action, username, password, verbose):
)
else:
ctx = click.get_current_context()
ctx.fail("No username/password provided")
ctx.fail("No username or password provided")

try:
if action == "login":
res = user.login()
resp = user.login()
message = f"{resp['username']} ({resp['online_ip']}) logged in"
elif action == "logout":
res = user.logout()
resp = user.logout()
message = f"{resp['online_ip']} logged out"
else:
# Should not reach here, but just in case
raise ValueError(f"Unknown action `{action}`")

# Output direct result of the API response if verbose
if verbose:
click.echo(f"{click.style('bitsrun:', fg='cyan')} Response from API:")
# click.echo(res)
pprint(res)
pprint(resp, indent=4)

# Handle error from API response. When field `error` is not `ok`, then the
# login/logout action has likely failed.
if res["error"] != "ok":
raise Exception(res["error"])
# login/logout action has likely failed. Hints are provided in the `error_msg`.
if resp["error"] != "ok":
raise Exception(
resp["error_msg"]
if resp["error_msg"]
else "Action failed, use --verbose for more info"
)

click.echo(
click.style("bitsrun: ", fg="green")
+ f"{res.get('username', user.username)} ({res['online_ip']}) logged in"
)
# Print success message
click.echo(f"{click.style('bitsrun:', fg='green')} {message}")

except Exception as e:
# Exception is caught and printed to stderr
click.echo(f"{click.style('error:', fg='red')} {e}", err=True)
# Throw with error code 1 for scripts to pick up error state
sys.exit(1)
Expand Down
2 changes: 1 addition & 1 deletion bitsrun/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class UserResponseType(TypedDict):
error: Union[Literal["login_error"], Literal["ok"]]
error_msg: str
res: Union[Literal["login_error"], Literal["ok"]]
# On login fails and all logout scenarios, field `username` is not present
# Field `username` is not present on login fails and all logout scenarios
username: Optional[str]


Expand Down

0 comments on commit 5761a86

Please sign in to comment.