Skip to content

Commit

Permalink
feat(auth): updates spinner and refactors existing code
Browse files Browse the repository at this point in the history
(cherry picked from commit faa7745)
  • Loading branch information
pallabpain committed Aug 3, 2023
1 parent 7184016 commit dd33d75
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 74 deletions.
11 changes: 6 additions & 5 deletions riocli/auth/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 Rapyuta Robotics
# Copyright 2023 Rapyuta Robotics
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -22,13 +22,14 @@
from riocli.auth.status import status
from riocli.auth.token import token
from riocli.config import new_client
from riocli.constants import Colors


@click.group(
invoke_without_command=False,
cls=HelpColorsGroup,
help_headers_color="yellow",
help_options_color="green",
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
def auth():
"""
Expand All @@ -43,7 +44,7 @@ def get_rio_client() -> Client:

auth.add_command(login)
auth.add_command(logout)
auth.add_command(status)
auth.add_command(refresh_token)
auth.add_command(token)
auth.add_command(status)
auth.add_command(environment)
auth.add_command(refresh_token)
21 changes: 12 additions & 9 deletions riocli/auth/login.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 Rapyuta Robotics
# Copyright 2023 Rapyuta Robotics
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -20,15 +20,17 @@
select_project,
validate_token,
)
from riocli.constants import Colors, Symbols
from riocli.utils.context import get_root_context

LOGIN_SUCCESS = click.style('Logged in successfully!', fg='green')
LOGIN_SUCCESS = click.style('{} Logged in successfully!'.format(Symbols.SUCCESS), fg=Colors.GREEN)


@click.command(
'login',
cls=HelpColorsCommand,
help_headers_color='yellow',
help_options_color='green',
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.option('--email', type=str,
help='Email of the rapyuta.io account')
Expand Down Expand Up @@ -88,9 +90,10 @@ def login(
if not ctx.obj.exists or not interactive:
ctx.obj.save()
else:
click.secho("[Warning] rio already has a config file present",
fg='yellow')
click.confirm('Do you want to override the config?', abort=True)
click.confirm(
'{} Config already exists. Do you want to override'
' the existing config?'.format(Symbols.WARNING),
abort=True)

if not interactive:
# When just the email and password are provided
Expand All @@ -105,7 +108,7 @@ def login(
if project and not organization:
click.secho(
'Please specify an organization. See `rio auth login --help`',
fg='yellow')
fg=Colors.YELLOW)
raise SystemExit(1)

# When just the organization is provided, we save the
Expand All @@ -114,7 +117,7 @@ def login(
if organization and not project:
select_organization(ctx.obj, organization=organization)
click.secho("Your organization is set to '{}'".format(
ctx.obj.data['organization_name']), fg='green')
ctx.obj.data['organization_name']), fg=Colors.CYAN)
ctx.obj.save()
click.echo(LOGIN_SUCCESS)
return
Expand Down
14 changes: 11 additions & 3 deletions riocli/auth/logout.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 Rapyuta Robotics
# Copyright 2023 Rapyuta Robotics
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,9 +12,17 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import click
from click_help_colors import HelpColorsCommand

from riocli.constants import Colors, Symbols

@click.command()

@click.command(
'logout',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.pass_context
def logout(ctx: click.Context):
"""
Expand All @@ -30,4 +38,4 @@ def logout(ctx: click.Context):
ctx.obj.data.pop('project_id', None)
ctx.obj.save()

click.secho('Logged out successfully!', fg='green')
click.secho('{} Logged out successfully.'.format(Symbols.SUCCESS), fg=Colors.GREEN)
19 changes: 14 additions & 5 deletions riocli/auth/refresh_token.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 Rapyuta Robotics
# Copyright 2023 Rapyuta Robotics
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,24 +12,33 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import click
from click_help_colors import HelpColorsCommand

from riocli.auth.util import get_token
from riocli.constants import Colors, Symbols
from riocli.exceptions import LoggedOut


@click.command()
@click.command(
'refresh-token',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.pass_context
def refresh_token(ctx: click.Context):
"""
Refreshes the authentication Token after it expires
Refreshes the authentication token after it expires
"""

email = ctx.obj.data.get('email_id', None)
password = ctx.obj.data.get('password', None)

if not ctx.obj.exists or not email or not password:
raise LoggedOut

ctx.obj.data['auth_token'] = get_token(email, password)

ctx.obj.save()
click.echo('Token refreshed successfully!')

click.secho('{} Token refreshed successfully!'.format(Symbols.SUCCESS),
fg=Colors.GREEN)
18 changes: 8 additions & 10 deletions riocli/auth/staging.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 Rapyuta Robotics
# Copyright 2023 Rapyuta Robotics
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@
from riocli.auth.login import select_project, select_organization
from riocli.auth.util import get_token
from riocli.config import Configuration
from riocli.constants import Colors, Symbols
from riocli.utils.context import get_root_context

_STAGING_ENVIRONMENT_SUBDOMAIN = "apps.okd4v2.okd4beta.rapyuta.io"
Expand All @@ -29,7 +30,6 @@ def environment(ctx: click.Context, name: str):
"""
Sets the Rapyuta.io environment to use (Internal use)
"""

ctx = get_root_context(ctx)

if name == 'ga':
Expand All @@ -50,18 +50,16 @@ def environment(ctx: click.Context, name: str):

organization = select_organization(ctx.obj)
select_project(ctx.obj, organization=organization)
ctx.obj.save()


def _validate_environment(name: str) -> bool:
valid = name in _NAMED_ENVIRONMENTS or name.startswith('pr')
if not valid:
click.secho('Invalid staging environment!', fg='red')
raise SystemExit(1)
ctx.obj.save()


def _configure_environment(config: Configuration, name: str) -> None:
_validate_environment(name)
is_valid_env = name in _NAMED_ENVIRONMENTS or name.startswith('pr')

if not is_valid_env:
click.secho('{} Invalid environment: {}'.format(Symbols.ERROR, name), fg=Colors.RED)
raise SystemExit(1)

catalog = 'https://{}catalog.{}'.format(name, _STAGING_ENVIRONMENT_SUBDOMAIN)
core = 'https://{}apiserver.{}'.format(name, _STAGING_ENVIRONMENT_SUBDOMAIN)
Expand Down
19 changes: 13 additions & 6 deletions riocli/auth/status.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 Rapyuta Robotics
# Copyright 2023 Rapyuta Robotics
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,18 +12,25 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import click
from click_help_colors import HelpColorsCommand

from riocli.constants import Colors

@click.command()

@click.command(
'status',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.pass_context
def status(ctx: click.Context):
"""
Shows the Login status of the CLI
Shows the login status of the CLI
"""

if not ctx.obj.exists:
click.secho('Logged out 🔒', fg='red')
click.secho('🔒You are logged out', fg=Colors.YELLOW)
raise SystemExit(1)

if 'auth_token' in ctx.obj.data:
click.secho('Logged in 🎉', fg='green')
click.secho('🎉 You are logged in', fg=Colors.GREEN)
19 changes: 14 additions & 5 deletions riocli/auth/token.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 Rapyuta Robotics
# Copyright 2023 Rapyuta Robotics
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,13 +12,20 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import click
from click_help_colors import HelpColorsCommand

from riocli.auth.util import get_token, TOKEN_LEVELS
from riocli.config import Configuration
from riocli.constants import Colors
from riocli.exceptions import LoggedOut


@click.command()
@click.command(
'token',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.option("--email", default=None, help="Email of the Rapyuta.io account")
@click.option("--password", default=None, hide_input=True,
help="Password for the Rapyuta.io account")
Expand All @@ -31,8 +38,9 @@ def token(email: str, password: str, level: int = 0):
config = Configuration()

if level not in TOKEN_LEVELS:
click.secho('Invalid token level. Valid levels are {0}'.format(
list(TOKEN_LEVELS.keys())), fg='red')
click.secho(
'Invalid token level. Valid levels are {0}'.format(
list(TOKEN_LEVELS.keys())), fg=Colors.RED)
raise SystemExit(1)

if not email:
Expand All @@ -44,4 +52,5 @@ def token(email: str, password: str, level: int = 0):
if not config.exists or not email or not password:
raise LoggedOut

click.echo(get_token(email, password, level))
new_token = get_token(email, password)
click.echo(new_token)
Loading

0 comments on commit dd33d75

Please sign in to comment.