Skip to content

afnio.tellurio.cli

afnio.tellurio.cli.afnio_echo(message, *args, fg=None, **kwargs)

Print a message to the console with a specific prefix.

Parameters:

Name Type Description Default
message str

The message to print.

required
*args Any

Additional arguments to pass to click.secho.

()
fg str

The foreground color for the message.

None
**kwargs Any

Additional keyword arguments to pass to click.secho.

{}
Source code in afnio/tellurio/cli.py
12
13
14
15
16
17
18
19
20
21
22
def afnio_echo(message: str, *args: Any, fg: str = None, **kwargs: Any):
    """Print a message to the console with a specific prefix.

    Args:
        message: The message to print.
        *args: Additional arguments to pass to click.secho.
        fg: The foreground color for the message.
        **kwargs: Additional keyword arguments to pass to click.secho.
    """
    prefix = click.style("[afnio] ", fg="blue")
    click.secho(prefix + message, fg=fg, *args, **kwargs)

afnio.tellurio.cli.cli(verbosity)

Command-line interface for afnio.

This CLI provides commands to authenticate with the Tellurio Studio platform and manage local configuration. Global options such as logging verbosity apply to all subcommands.

Source code in afnio/tellurio/cli.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
@click.group()
@click.option(
    "--verbosity",
    "-v",
    default="warning",
    show_default=True,
    type=click.Choice(
        ["debug", "info", "warning", "error", "critical"], case_sensitive=False
    ),
    help="Set the logging verbosity level.",
)
def cli(verbosity):
    """Command-line interface for `afnio`.

    This CLI provides commands to authenticate with the
    [Tellurio Studio](https://platform.tellurio.ai/) platform and manage
    local configuration. Global options such as logging verbosity
    apply to all subcommands.
    """
    configure_logging(verbosity)

afnio.tellurio.cli.login(api_key=None, relogin=False)

Log in to Tellurio using an API key.

This command allows you to authenticate with the Tellurio platform using your API key. If the API key is already stored in the system's keyring, it will be used automatically without prompting the user.

Parameters:

Name Type Description Default
api_key str

The user's API key for authentication.

None
relogin bool

If True, forces a re-login and requires a new API key.

False

Raises:

Type Description
InvalidAPIKeyError

If the API key is invalid.

RuntimeError

If the WebSocket connection fails after successful authentication.

ValueError

If the API key is not provided during re-login.

Exception

For any other unexpected errors during login.

Source code in afnio/tellurio/cli.py
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
@cli.command()
@click.option("--api-key", help="Your API key.", required=False, hide_input=True)
@click.option(
    "--relogin", is_flag=True, help="Force a re-login and request a new API key."
)
def login(api_key: str = None, relogin: bool = False):
    """Log in to Tellurio using an API key.

    This command allows you to authenticate with the Tellurio platform using
    your API key. If the API key is already stored in the system's keyring,
    it will be used automatically without prompting the user.

    Args:
        api_key: The user's API key for authentication.
        relogin: If `True`, forces a re-login and requires a new API key.

    Raises:
        afnio.tellurio.client.InvalidAPIKeyError: If the API key is invalid.
        RuntimeError: If the WebSocket connection fails after successful authentication.
        ValueError: If the API key is not provided during re-login.
        Exception: For any other unexpected errors during login.
    """
    username = load_username()
    service_name = os.getenv("KEYRING_SERVICE_NAME", "Tellurio")

    try:
        # Check if the API key is already stored in the keyring
        stored_api_key = (
            keyring.get_password(service_name, username) if username else None
        )

        # Decide which API key to use
        if stored_api_key and not relogin:
            # If stored key is available and not forcing relogin,
            # we let module_login() to retrieve it internally
            api_key_to_use = None
            afnio_echo("Using stored API key from local keyring.")
        else:
            # Use provided api_key or prompt the user
            if not api_key:
                api_key = click.prompt(
                    "Please enter your Tellurio API key and hit enter, "
                    "or press ctrl+c to quit",
                    hide_input=True,
                )
            api_key_to_use = api_key

        # Log in to the Tellurio platform
        response = module_login(api_key=api_key_to_use, relogin=relogin)

        if api_key_to_use:
            afnio_echo("API key provided and stored securely in local keyring.")

        base_url = os.getenv(
            "TELLURIO_BACKEND_HTTP_BASE_URL", "https://platform.tellurio.ai"
        )

        username_str = click.style(repr(response["username"]), fg="yellow")
        base_url_str = click.style(repr(base_url), fg="green")
        afnio_echo(
            f"Currently logged in as {username_str} to {base_url_str}. "
            f"Use `afnio login --relogin` to force relogin."
        )
    except ValueError:
        afnio_echo("Login failed: Missing API key. Please provide a valid API key.")
    except InvalidAPIKeyError:
        afnio_echo("Login failed: Invalid API key. Please try again.")
    except RuntimeError:
        afnio_echo("Login failed: Failed to connect to the backend.")
    except Exception:
        afnio_echo("An unexpected error occurred. Please try again.")