-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Config] Make config items case-insensitive #220
Conversation
knack/config.py
Outdated
for config in self._config_file_chain if self.use_local_config else self._config_file_chain[-1:]: | ||
try: | ||
entries = config.items(section) | ||
for name, value in entries: | ||
if name not in result: | ||
if name.lower() not in result: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should force lower case for all configs in files, otherwise we need to change get
and set
as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall we add force rule in set_value
?
knack/config.py
Outdated
@@ -109,9 +109,9 @@ def sections(self): | |||
|
|||
def items(self, section): | |||
import re | |||
pattern = self.env_var_name(section, '.+') | |||
pattern = self.env_var_name(section, '[0-9A-Z]+') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The option name can consist of multiple parts, like AZURE_BATCHAI_STORAGE_ACCOUNT
.
knack/config.py
Outdated
candidates = [(k.split('_')[-1], os.environ[k], k) for k in os.environ if re.match(pattern, k)] | ||
result = {c[0]: c for c in candidates} | ||
pattern = self.env_var_name(section, '[0-9A-Z][0-9A-Z_]*') | ||
candidates = [(k.replace(self.env_var_name(section, ''), ''), os.environ[k], k) for k in os.environ if re.match(pattern, k)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replace
can cause unwanted effect if self.env_var_name(section, '')
somehow appears in the middle of the env var. removeprefix
is a better choice but it is only available since Python 3.9.
Better to use grouping of regex.
tests/test_config.py
Outdated
envOption = '{}_{}_{}'.format(CLIConfig._DEFAULT_CONFIG_ENV_VAR_PREFIX, section, 'TEST_OPTION') | ||
envVaule = 'envValue' | ||
configOption = 'test_option' | ||
configValue = 'value' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The python convention is to use snake case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The python convention is to use snake case.
Agree
knack/config.py
Outdated
pattern = self.env_var_name(section, '.+') | ||
candidates = [(k.split('_')[-1], os.environ[k], k) for k in os.environ if re.match(pattern, k)] | ||
result = {c[0]: c for c in candidates} | ||
pattern = self.env_var_name(section, '[0-9A-Z][0-9A-Z_]*') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will match CLI_MYSECTION_T
part in CLI_MYSECTION_Test_Option
, defeating our purpose of ignoring invalid env options like CLI_MYSECTION_Test_Option
. We should use fullmatch
instead.
looks good now |
Description
Fix Azure/azure-cli#14397: az configure --list-defaults should not be case sensitive for environment variables
Testing Guide
preparation(need Azure CLI in advance)
az configure --defaults location=eastus export AZURE_DEFAULTS_LOCATION=eastasia
test
results before
results after