1616# We use prompt_toolkit for major prompts
1717# and click or simple code for others.
1818
19+ import os
1920import click
2021from typing import Optional
2122
2223# from prompt_toolkit.shortcuts import input_dialog, radiolist_dialog, yes_no_dialog
2324
24- # from planet_auth_utils.builtins import Builtins
2525from planet_auth_utils .plauth_user_config import PlanetAuthUserConfigEnhanced
2626from planet_auth_utils .constants import EnvironmentVariables
2727
2828
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+
2954def prompt_and_change_user_default_profile_if_different (
3055 candidate_profile_name : str , change_default_selection : Optional [bool ] = None
3156):
@@ -34,6 +59,7 @@ def prompt_and_change_user_default_profile_if_different(
3459 saved_profile_name = config_file .lazy_get (EnvironmentVariables .AUTH_PROFILE )
3560 except FileNotFoundError :
3661 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
3763
3864 if not saved_profile_name :
3965 # Always write a preference if none is saved.
@@ -56,7 +82,9 @@ def prompt_and_change_user_default_profile_if_different(
5682 )
5783
5884 if do_change_default :
85+ selected_profile_name = candidate_profile_name
5986 config_file .update_data ({EnvironmentVariables .AUTH_PROFILE : candidate_profile_name })
6087 config_file .save ()
6188
89+ _warn_env_overrides_selection (selected_profile_name )
6290 return do_change_default
0 commit comments