diff --git a/planet/cli/auth.py b/planet/cli/auth.py index 49e0bc937..76714f9b8 100644 --- a/planet/cli/auth.py +++ b/planet/cli/auth.py @@ -17,6 +17,7 @@ import click import planet +from planet.exceptions import AuthException from .cmds import translate_exceptions LOGGER = logging.getLogger(__name__) @@ -53,7 +54,11 @@ def init(ctx, email, password): @auth.command() +@click.pass_context @translate_exceptions -def value(): +def value(ctx): '''Print the stored authentication information''' - click.echo(planet.Auth.from_file().value) + try: + click.echo(ctx.obj['AUTH'].value) + except (AttributeError, KeyError): + raise AuthException('Command context lacks auth information.') diff --git a/planet/cli/cli.py b/planet/cli/cli.py index d75eb339e..82d2ee4d7 100644 --- a/planet/cli/cli.py +++ b/planet/cli/cli.py @@ -19,6 +19,8 @@ import click import planet +from planet.auth import Auth +from planet.exceptions import PlanetError from . import auth, collect, data, orders, subscriptions @@ -45,6 +47,20 @@ def main(ctx, verbosity, quiet): ctx.ensure_object(dict) ctx.obj['QUIET'] = quiet + # Check authentication sources and store the highest priority in ctx. + # 1. Environment. + try: + auth_env = Auth.from_env() + except PlanetError: + auth_env = None + # 2. Secret file. + try: + auth_file = Auth.from_file() + except PlanetError: + auth_file = None + + ctx.obj['AUTH'] = auth_env or auth_file + def _configure_logging(verbosity): """configure logging via verbosity level, corresponding diff --git a/planet/cli/data.py b/planet/cli/data.py index 5588c6d9d..f9f6a1aaf 100644 --- a/planet/cli/data.py +++ b/planet/cli/data.py @@ -48,7 +48,6 @@ async def data_client(ctx): help='Assign custom base Orders API URL.') def data(ctx, base_url): '''Commands for interacting with the Data API''' - ctx.obj['AUTH'] = None ctx.obj['BASE_URL'] = base_url diff --git a/planet/cli/orders.py b/planet/cli/orders.py index 73138d54a..ab09ab84e 100644 --- a/planet/cli/orders.py +++ b/planet/cli/orders.py @@ -46,7 +46,6 @@ async def orders_client(ctx): help='Assign custom base Orders API URL.') def orders(ctx, base_url): '''Commands for interacting with the Orders API''' - ctx.obj['AUTH'] = None ctx.obj['BASE_URL'] = base_url diff --git a/planet/cli/subscriptions.py b/planet/cli/subscriptions.py index 42a4dadc7..bceea55ca 100644 --- a/planet/cli/subscriptions.py +++ b/planet/cli/subscriptions.py @@ -14,9 +14,6 @@ @click.pass_context def subscriptions(ctx): '''Commands for interacting with the Subscriptions API''' - # None means that order of precedence is 1) environment variable, - # 2) secret file. - ctx.obj['AUTH'] = None # We want our command to be known as "list" on the command line but