Skip to content

Commit

Permalink
feat(auth): adds support for non-interactive login (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitrgadiya authored May 25, 2022
1 parent 09bb128 commit 21730c5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
30 changes: 24 additions & 6 deletions riocli/auth/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,48 @@
help_headers_color='yellow',
help_options_color='green',
)
@click.option('--email', prompt='Email',
@click.option('--email', type=str,
help='Email of the Rapyuta.io account')
@click.option('--password', prompt='Password', hide_input=True,
@click.option('--password', type=str,
help='Password for the Rapyuta.io account')
@click.option('--project', type=str, default=None,
help='Context will be set to the Project after authentication')
@click.option('--interactive/--no-interactive', is_flag=True, type=bool, default=True,
help='Make login interactive')
@click.pass_context
def login(ctx: click.Context, email: str, password: str):
def login(ctx: click.Context, email: str, password: str, project: str, interactive: bool):
"""
Log into the Rapyuta.io account using the CLI. This is required to use most of the
functionalities of the CLI.
"""

if interactive:
email = email or click.prompt('Email')
password = password or click.prompt('Password', hide_input=True)

if not email:
click.secho('email not specified')
exit(1)
if not password:
click.secho('password not specified')
exit(1)

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

# Save if the file does not already exist
if not ctx.obj.exists:
click.echo('Logged in successfully!')
if not ctx.obj.exists or not interactive:
ctx.obj.save()
else:
click.echo("[Warning] rio already has a config file present")
click.confirm('Do you want to override the config', abort=True)

select_project(ctx.obj)
if not interactive and not project:
click.echo('Logged in successfully!')
return

select_project(ctx.obj, project=project)
ctx.obj.save()
click.echo('Logged in successfully!')
18 changes: 13 additions & 5 deletions riocli/auth/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,31 @@

from riocli.config import Configuration
from riocli.utils.selector import show_selection
from riocli.project.util import find_project_guid


def select_project(config: Configuration) -> str:
def select_project(config: Configuration, project: str = None) -> None:
"""
Launches the project selection prompt by listing all the projects.
Sets the choice in the given configuration.
"""
client = config.new_client(with_project=False)
projects = client.list_projects()

project_guid = None
if project:
project_guid = project if project.startswith('project-') else find_project_guid(client, project)

projects = client.list_projects()
project_map = dict()

for project in projects:
project_map[project.guid] = project.name

choice = show_selection(project_map, header='Select the project to activate')
config.data['project_id'] = choice
config.data['project_name'] = project_map[choice]
if not project_guid:
project_guid = show_selection(project_map, header='Select the project to activate')

config.data['project_id'] = project_guid
config.data['project_name'] = project_map[project_guid]


def get_token(email: str, password: str) -> str:
Expand Down

0 comments on commit 21730c5

Please sign in to comment.