Commit d59c7102 authored by Amir MOHAMMADI's avatar Amir MOHAMMADI

[click] lookup in the config files before env var

parent 8f62fd08
Pipeline #36130 passed with stage
in 8 minutes and 6 seconds
...@@ -177,20 +177,6 @@ def verbosity_option(**kwargs): ...@@ -177,20 +177,6 @@ def verbosity_option(**kwargs):
return custom_verbosity_option return custom_verbosity_option
def configs_argument(cmd, config_argument_name, entry_point_group):
"""This a
Parameters
----------
cmd : TYPE
Description
config_argument_name : TYPE
Description
entry_point_group : TYPE
Description
"""
class ConfigCommand(click.Command): class ConfigCommand(click.Command):
"""A click.Command that can take options both form command line options and """A click.Command that can take options both form command line options and
configuration files. In order to use this class, you have to use the configuration files. In order to use this class, you have to use the
...@@ -204,14 +190,7 @@ class ConfigCommand(click.Command): ...@@ -204,14 +190,7 @@ class ConfigCommand(click.Command):
The name of entry point that will be used to load the config files. The name of entry point that will be used to load the config files.
""" """
def __init__( def __init__(self, name, *args, help=None, entry_point_group=None, **kwargs):
self,
name,
*args,
help=None,
entry_point_group=None,
**kwargs
):
self.entry_point_group = entry_point_group self.entry_point_group = entry_point_group
configs_argument_name = "CONFIG" configs_argument_name = "CONFIG"
# Augment help for the config file argument # Augment help for the config file argument
...@@ -378,13 +357,13 @@ class ResourceOption(click.Option): ...@@ -378,13 +357,13 @@ class ResourceOption(click.Option):
def consume_value(self, ctx, opts): def consume_value(self, ctx, opts):
logger.debug("consuming resource option for option %s", self.name) logger.debug("consuming resource option for option %s", self.name)
value = opts.get(self.name) value = opts.get(self.name)
# if value is not given from command line, lookup the environment variables # if value is not given from command line, lookup the config files
if value is None:
value = self.value_from_envvar(ctx)
# if not from environment variables, lookup the config files
if value is None: if value is None:
value = ctx.config_context.get(self.name) value = ctx.config_context.get(self.name)
# if not from config files, lookup the default value # if not from config files, lookup the environment variables
if value is None:
value = self.value_from_envvar(ctx)
# if not from environment variables, lookup the default value
if value is None: if value is None:
value = ctx.lookup_default(self.name) value = ctx.lookup_default(self.name)
return value return value
......
...@@ -257,7 +257,7 @@ def test_config_dump2(): ...@@ -257,7 +257,7 @@ def test_config_dump2():
def test_config_command_with_callback_options(): def test_config_command_with_callback_options():
@click.command(cls=ConfigCommand, entry_point_group='bob.extension.test_config_load') @click.command(cls=ConfigCommand, entry_point_group='bob.extension.test_config_load')
@verbosity_option(cls=ResourceOption) @verbosity_option(cls=ResourceOption, envvar='VERBOSE')
@click.pass_context @click.pass_context
def cli(ctx, **kwargs): def cli(ctx, **kwargs):
verbose = ctx.meta['verbosity'] verbose = ctx.meta['verbosity']
...@@ -266,3 +266,11 @@ def test_config_command_with_callback_options(): ...@@ -266,3 +266,11 @@ def test_config_command_with_callback_options():
runner = CliRunner() runner = CliRunner()
result = runner.invoke(cli, ['verbose_config'], catch_exceptions=False) result = runner.invoke(cli, ['verbose_config'], catch_exceptions=False)
assert_click_runner_result(result) assert_click_runner_result(result)
runner = CliRunner(env=dict(VERBOSE='1'))
result = runner.invoke(cli, ['verbose_config'], catch_exceptions=False)
assert_click_runner_result(result)
runner = CliRunner(env=dict(VERBOSE='2'))
result = runner.invoke(cli, catch_exceptions=False)
assert_click_runner_result(result)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment