Skip to content

Commit de77851

Browse files
Beta feedback 1 - login command ergonomics - Initial cleanup (#31)
* avoid degenerate variable names * allow env var to be None for click CLI options. * profile names are not case sensitive. Avoid silly prompts. * Make what configs are consulted configurable
1 parent f6adca4 commit de77851

File tree

4 files changed

+35
-20
lines changed

4 files changed

+35
-20
lines changed

src/planet_auth_utils/commands/cli/options.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import click
1616
import pathlib
17-
from typing import Any, Callable
17+
from typing import Any, Callable, Optional
1818

1919
from planet_auth_utils.constants import EnvironmentVariables
2020

@@ -25,7 +25,7 @@
2525
# TODO: Should we make "required" param universal for all options?
2626
# Maybe rather than being so prescriptive, we pass **kwargs to click options?
2727
def opt_api_key(
28-
default=None, hidden: bool = False, envvar: str = EnvironmentVariables.AUTH_API_KEY
28+
default=None, hidden: bool = False, envvar: Optional[str] = EnvironmentVariables.AUTH_API_KEY
2929
) -> _click_option_decorator_type:
3030
"""
3131
Click option for specifying an API key
@@ -48,7 +48,7 @@ def decorator(function) -> _click_option_decorator_type:
4848

4949

5050
def opt_audience(
51-
default=None, hidden: bool = False, required=False, envvar: str = EnvironmentVariables.AUTH_AUDIENCE
51+
default=None, hidden: bool = False, required=False, envvar: Optional[str] = EnvironmentVariables.AUTH_AUDIENCE
5252
) -> _click_option_decorator_type:
5353
"""
5454
Click option for specifying an OAuth token audience for the
@@ -76,7 +76,7 @@ def decorator(function) -> _click_option_decorator_type:
7676

7777

7878
def opt_client_id(
79-
default=None, hidden: bool = False, envvar: str = EnvironmentVariables.AUTH_CLIENT_ID
79+
default=None, hidden: bool = False, envvar: Optional[str] = EnvironmentVariables.AUTH_CLIENT_ID
8080
) -> _click_option_decorator_type:
8181
"""
8282
Click option for specifying an OAuth client ID.
@@ -99,7 +99,7 @@ def decorator(function) -> _click_option_decorator_type:
9999

100100

101101
def opt_client_secret(
102-
default=None, hidden: bool = False, envvar: str = EnvironmentVariables.AUTH_CLIENT_SECRET
102+
default=None, hidden: bool = False, envvar: Optional[str] = EnvironmentVariables.AUTH_CLIENT_SECRET
103103
) -> _click_option_decorator_type:
104104
"""
105105
Click option for specifying an OAuth client secret.
@@ -122,7 +122,7 @@ def decorator(function) -> _click_option_decorator_type:
122122

123123

124124
def opt_extra(
125-
default=None, hidden: bool = False, envvar: str = EnvironmentVariables.AUTH_EXTRA
125+
default=None, hidden: bool = False, envvar: Optional[str] = EnvironmentVariables.AUTH_EXTRA
126126
) -> _click_option_decorator_type:
127127
"""
128128
Click option for specifying extra options.
@@ -169,7 +169,7 @@ def decorator(function) -> _click_option_decorator_type:
169169

170170

171171
def opt_issuer(
172-
default=None, hidden: bool = False, required=False, envvar: str = EnvironmentVariables.AUTH_ISSUER
172+
default=None, hidden: bool = False, required=False, envvar: Optional[str] = EnvironmentVariables.AUTH_ISSUER
173173
) -> _click_option_decorator_type:
174174
"""
175175
Click option for specifying an OAuth token issuer for the
@@ -194,7 +194,7 @@ def decorator(function) -> _click_option_decorator_type:
194194

195195

196196
def opt_loglevel(
197-
default="INFO", hidden: bool = False, envvar: str = EnvironmentVariables.AUTH_LOGLEVEL
197+
default="INFO", hidden: bool = False, envvar: Optional[str] = EnvironmentVariables.AUTH_LOGLEVEL
198198
) -> _click_option_decorator_type:
199199
"""
200200
Click option for specifying a log level.
@@ -257,7 +257,7 @@ def decorator(function) -> _click_option_decorator_type:
257257

258258

259259
def opt_organization(
260-
default=None, hidden: bool = False, envvar: str = EnvironmentVariables.AUTH_ORGANIZATION
260+
default=None, hidden: bool = False, envvar: Optional[str] = EnvironmentVariables.AUTH_ORGANIZATION
261261
) -> _click_option_decorator_type:
262262
"""
263263
Click option for specifying an Organization.
@@ -287,7 +287,7 @@ def decorator(function) -> _click_option_decorator_type:
287287
# lib also handles things like browser interaction this is not entirely easy to abstract
288288
# away.
289289
def opt_password(
290-
default=None, hidden: bool = True, envvar: str = EnvironmentVariables.AUTH_PASSWORD
290+
default=None, hidden: bool = True, envvar: Optional[str] = EnvironmentVariables.AUTH_PASSWORD
291291
) -> _click_option_decorator_type:
292292
"""
293293
Click option for specifying a password for the
@@ -311,7 +311,7 @@ def decorator(function) -> _click_option_decorator_type:
311311

312312

313313
def opt_profile(
314-
default=None, hidden: bool = False, envvar: str = EnvironmentVariables.AUTH_PROFILE
314+
default=None, hidden: bool = False, envvar: Optional[str] = EnvironmentVariables.AUTH_PROFILE
315315
) -> _click_option_decorator_type:
316316
"""
317317
Click option for specifying an auth profile for the
@@ -336,7 +336,7 @@ def decorator(function) -> _click_option_decorator_type:
336336

337337

338338
def opt_project(
339-
default=None, hidden: bool = False, envvar: str = EnvironmentVariables.AUTH_PROJECT
339+
default=None, hidden: bool = False, envvar: Optional[str] = EnvironmentVariables.AUTH_PROJECT
340340
) -> _click_option_decorator_type:
341341
"""
342342
Click option for specifying a project ID.
@@ -397,7 +397,7 @@ def decorator(function) -> _click_option_decorator_type:
397397

398398

399399
def opt_token(
400-
default=None, hidden: bool = False, envvar: str = EnvironmentVariables.AUTH_TOKEN
400+
default=None, hidden: bool = False, envvar: Optional[str] = EnvironmentVariables.AUTH_TOKEN
401401
) -> _click_option_decorator_type:
402402
"""
403403
Click option for specifying a token literal.
@@ -421,7 +421,7 @@ def decorator(function) -> _click_option_decorator_type:
421421

422422

423423
def opt_scope(
424-
default=None, hidden: bool = False, envvar: str = EnvironmentVariables.AUTH_SCOPE
424+
default=None, hidden: bool = False, envvar: Optional[str] = EnvironmentVariables.AUTH_SCOPE
425425
) -> _click_option_decorator_type:
426426
"""
427427
Click option for specifying an OAuth token scope for the
@@ -468,7 +468,7 @@ def decorator(function) -> _click_option_decorator_type:
468468

469469

470470
def opt_token_file(
471-
default=None, hidden: bool = False, envvar: str = EnvironmentVariables.AUTH_TOKEN_FILE
471+
default=None, hidden: bool = False, envvar: Optional[str] = EnvironmentVariables.AUTH_TOKEN_FILE
472472
) -> _click_option_decorator_type:
473473
"""
474474
Click option for specifying a token file location for the
@@ -492,7 +492,7 @@ def decorator(function) -> _click_option_decorator_type:
492492

493493

494494
def opt_username(
495-
default=None, hidden: bool = True, envvar: str = EnvironmentVariables.AUTH_USERNAME
495+
default=None, hidden: bool = True, envvar: Optional[str] = EnvironmentVariables.AUTH_USERNAME
496496
) -> _click_option_decorator_type:
497497
"""
498498
Click option for specifying a username for the

src/planet_auth_utils/commands/cli/prompts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def prompt_and_change_user_default_profile_if_different(
4444
do_change_default = change_default_selection
4545
else:
4646
do_change_default = False
47-
if saved_profile_name != candidate_profile_name:
47+
if str.lower(saved_profile_name) != str.lower(candidate_profile_name):
4848
# do_change_default = yes_no_dialog(
4949
# title=f'Change user default {EnvironmentVariables.AUTH_PROFILE} saved in {config_file.path()}?',
5050
# text=f'Current value and user default differ.\nDo you want to change the user default {EnvironmentVariables.AUTH_PROFILE} from "{saved_profile_name}" to "{candidate_profile_name}"?',

src/planet_auth_utils/constants.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from typing import Optional
16+
1517
from planet_auth_utils.builtins import Builtins
1618

1719

@@ -29,15 +31,15 @@ class EnvironmentVariables:
2931
"""
3032

3133
@staticmethod
32-
def _namespace_variable(undecorated_variable: str):
34+
def _namespace_variable(undecorated_variable: Optional[str]):
3335
"""
3436
Decorate the variable name with a namespace.
3537
This is done so that multiple applications may use
3638
the Planet auth library without conflicting.
3739
"""
3840

3941
namespace = Builtins.namespace()
40-
if namespace:
42+
if namespace and undecorated_variable:
4143
return f"{namespace.upper()}_{undecorated_variable}"
4244
return undecorated_variable
4345

src/planet_auth_utils/plauth_factory.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ def initialize_auth_client_context(
243243
# TODO?: initial_token_data: dict = None,
244244
save_token_file: bool = True,
245245
save_profile_config: bool = False,
246+
use_env: bool = True,
247+
use_configfile: bool = True,
246248
# Not supporting custom storage providers at this time.
247249
# The preferred behavior of Profiles with custom storage providers is TBD.
248250
# storage_provider: Optional[ObjectStorageProvider] = None,
@@ -312,6 +314,8 @@ def my_cli_main(ctx, auth_profile, auth_client_id, auth_client_secret):
312314
save_token_file: Whether to save the access token to disk. If `False`, in-memory
313315
operation will be used, and login sessions will not be persisted locally.
314316
save_profile_config: Whether to save the profile configuration to disk.
317+
use_env: Whether to use environment variables to determine configuration values.
318+
use_configfile: Whether to use configuration files to determine configuration values.
315319
"""
316320
#
317321
# Initialize from explicit user selected options
@@ -346,6 +350,8 @@ def my_cli_main(ctx, auth_profile, auth_client_id, auth_client_secret):
346350
effective_user_selected_profile = user_config_file.effective_conf_value(
347351
config_key=EnvironmentVariables.AUTH_PROFILE,
348352
override_value=auth_profile_opt,
353+
use_env=use_env,
354+
use_configfile=use_configfile,
349355
)
350356
if effective_user_selected_profile:
351357
try:
@@ -365,10 +371,14 @@ def my_cli_main(ctx, auth_profile, auth_client_id, auth_client_secret):
365371
effective_user_selected_client_id = user_config_file.effective_conf_value(
366372
config_key=EnvironmentVariables.AUTH_CLIENT_ID,
367373
override_value=auth_client_id_opt,
374+
use_env=use_env,
375+
use_configfile=use_configfile,
368376
)
369377
effective_user_selected_client_secret = user_config_file.effective_conf_value(
370378
config_key=EnvironmentVariables.AUTH_CLIENT_SECRET,
371379
override_value=auth_client_secret_opt,
380+
use_env=use_env,
381+
use_configfile=use_configfile,
372382
)
373383
if effective_user_selected_client_id and effective_user_selected_client_secret:
374384
return PlanetAuthFactory._init_context_from_oauth_svc_account(
@@ -383,16 +393,19 @@ def my_cli_main(ctx, auth_profile, auth_client_id, auth_client_secret):
383393
effective_user_selected_api_key = user_config_file.effective_conf_value(
384394
config_key=EnvironmentVariables.AUTH_API_KEY,
385395
override_value=auth_api_key_opt,
396+
use_env=use_env,
397+
use_configfile=use_configfile,
386398
)
387399
if effective_user_selected_api_key:
388400
return PlanetAuthFactory._init_context_from_api_key(
389401
api_key=effective_user_selected_api_key,
390402
)
391403

392404
effective_user_selected_api_key = user_config_file.effective_conf_value(
393-
config_key="key", # For backwards compatibility
405+
config_key="key", # For backwards compatibility, we know the old SDK used this in json files.
394406
override_value=auth_api_key_opt,
395407
use_env=False,
408+
use_configfile=use_configfile,
396409
)
397410
if effective_user_selected_api_key:
398411
return PlanetAuthFactory._init_context_from_api_key(

0 commit comments

Comments
 (0)