16
16
# We use prompt_toolkit for major prompts
17
17
# and click or simple code for others.
18
18
19
+ import os
19
20
import click
20
21
from typing import Optional
21
22
22
23
# from prompt_toolkit.shortcuts import input_dialog, radiolist_dialog, yes_no_dialog
23
24
24
- # from planet_auth_utils.builtins import Builtins
25
25
from planet_auth_utils .plauth_user_config import PlanetAuthUserConfigEnhanced
26
26
from planet_auth_utils .constants import EnvironmentVariables
27
27
28
28
29
+ def _warn_env_overrides_selection (selected_profile_name : str ):
30
+ # If we detect a current environment that we expect will override
31
+ # what is being saved to the config file, warn the user.
32
+ # See https://github.com/pylint-dev/pylint/issues/5091 regarding the pylint disabled warning.
33
+ env_profile = os .getenv (EnvironmentVariables .AUTH_PROFILE , None ) # pylint: disable=E1507
34
+ if env_profile and str .lower (env_profile ) != str .lower (selected_profile_name ):
35
+ print (
36
+ f'Warning: Environment variable "{ EnvironmentVariables .AUTH_PROFILE } " is set to "{ env_profile } ". This will override saved settings.'
37
+ )
38
+
39
+ env_client_id = os .getenv (EnvironmentVariables .AUTH_CLIENT_ID , None ) # pylint: disable=E1507
40
+ env_client_secret = os .getenv (EnvironmentVariables .AUTH_CLIENT_SECRET , None ) # pylint: disable=E1507
41
+ if env_client_id and env_client_secret :
42
+ print (
43
+ f'Warning: Environment variables "{ EnvironmentVariables .AUTH_CLIENT_ID } " and "{ EnvironmentVariables .AUTH_CLIENT_SECRET } " are set.'
44
+ " These will always take priority over the saved settings."
45
+ )
46
+
47
+ env_api_key = os .getenv (EnvironmentVariables .AUTH_API_KEY , None ) # pylint: disable=E1507
48
+ if env_api_key : # and str.lower(_PL_API_KEY_ADHOC_PROFILE_NAME) != str.lower(selected_profile_name):
49
+ print (
50
+ f'Warning: Environment variable "{ EnvironmentVariables .AUTH_API_KEY } " is set. This will always take priority over the saved settings.'
51
+ )
52
+
53
+
29
54
def prompt_and_change_user_default_profile_if_different (
30
55
candidate_profile_name : str , change_default_selection : Optional [bool ] = None
31
56
):
@@ -34,6 +59,7 @@ def prompt_and_change_user_default_profile_if_different(
34
59
saved_profile_name = config_file .lazy_get (EnvironmentVariables .AUTH_PROFILE )
35
60
except FileNotFoundError :
36
61
saved_profile_name = None # config_file.effective_conf_value(EnvironmentVariables.AUTH_PROFILE, fallback_value=Builtins.builtin_default_profile_name())
62
+ selected_profile_name = saved_profile_name
37
63
38
64
if not saved_profile_name :
39
65
# Always write a preference if none is saved.
@@ -56,7 +82,9 @@ def prompt_and_change_user_default_profile_if_different(
56
82
)
57
83
58
84
if do_change_default :
85
+ selected_profile_name = candidate_profile_name
59
86
config_file .update_data ({EnvironmentVariables .AUTH_PROFILE : candidate_profile_name })
60
87
config_file .save ()
61
88
89
+ _warn_env_overrides_selection (selected_profile_name )
62
90
return do_change_default
0 commit comments