-
Notifications
You must be signed in to change notification settings - Fork 3
estela-cli: Add command to save different contexts #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,3 +12,4 @@ venv/ | |
| .venv/ | ||
| .DS_Store | ||
| site/ | ||
| .vscode/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,21 @@ | ||
| import yaml | ||
| import click | ||
| import requests | ||
|
|
||
| from getpass import getpass | ||
| from estela_cli.login import env_login, yaml_login | ||
| from estela_cli.estela_client import EstelaClient | ||
| from estela_cli.utils import ( | ||
| get_estela_auth, | ||
| update_estela_auth, | ||
| get_estela_settings, | ||
| get_estela_config, | ||
| get_host_from_env, | ||
| get_username_from_env, | ||
| get_password_from_env, | ||
| get_estela_config_path, | ||
| ) | ||
| from estela_cli.templates import ESTELA_AUTH_NAME, OK_EMOJI, BAD_EMOJI | ||
| from estela_cli.templates import ESTELA_AUTH_NAME, ESTELA_CONFIG_NAME, OK_EMOJI, BAD_EMOJI | ||
|
|
||
|
|
||
| SHORT_HELP = "Show your current context" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should update the description of the command here to also indicate that it serves to update/change the context |
||
|
|
@@ -30,12 +36,73 @@ def test_host(host): | |
| assert "projects" in response.json() | ||
|
|
||
|
|
||
| def prompt_context(name, username=None, password=None, host=None): | ||
| try: | ||
| if host is None: | ||
| host = click.prompt("Host") | ||
| if username is None: | ||
| username = click.prompt("Username") | ||
| if password is None: | ||
| password = getpass() | ||
|
|
||
| estela_config_path = get_estela_config_path() | ||
| estela_config = get_estela_config() | ||
|
|
||
| if estela_config is None: | ||
| estela_config = {} | ||
|
|
||
| estela_config[name] = { | ||
| "host": host, | ||
| "username": username, | ||
| "password": password, | ||
| } | ||
| with open(estela_config_path, "w") as estela_config_yaml: | ||
| yaml.dump(estela_config, estela_config_yaml) | ||
| click.echo( | ||
| "Successful login. Context {} stored in ~/{}.".format( | ||
| name, ESTELA_CONFIG_NAME | ||
| ) | ||
| ) | ||
| estela_client = EstelaClient(host, username, password) | ||
|
|
||
| except: | ||
| raise Exception("Unable to login with provided credentials.") | ||
|
|
||
| return estela_client | ||
|
|
||
|
|
||
| @click.command(name="context", short_help=SHORT_HELP) | ||
| def estela_command(): | ||
| @click.argument("name", required=False) | ||
| def estela_command(name): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a docstring explaining what we can do with the command. You can see other commands such as |
||
| host = get_host_from_env() | ||
| username = get_username_from_env() | ||
| password = get_password_from_env() | ||
|
|
||
| if name: | ||
| click.echo(f"Checking your current context for {name}...") | ||
| estela_config = get_estela_config() | ||
|
|
||
| if estela_config and estela_config.get(name, None): | ||
| click.echo("Context {} found.".format(name)) | ||
| try: | ||
| test_host(estela_config[name]["host"]) | ||
| click.echo(OK_HOST.format(estela_config[name]["host"])) | ||
| estela_client = EstelaClient( | ||
| estela_config[name]["host"], | ||
| estela_config[name]["username"], | ||
| estela_config[name]["password"], | ||
| ) | ||
| except: | ||
| click.echo(BAD_HOST) | ||
| return | ||
| else: | ||
| click.echo( | ||
| "Context {} not found.\nInitializing context...".format(name) | ||
| ) | ||
| estela_client = prompt_context(name, username, password, host) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I use incorrect credentials, I get this output: You can see "Successful login" and errors because I provided incorrect credentials. If the credentials are incorrect, the context should simply not be saved. |
||
| update_estela_auth(estela_client.host, estela_client.token) | ||
| return | ||
|
|
||
| if host is None or username is None or password is None: | ||
| estela_auth = get_estela_auth() | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please apply
blackto format the code