Skip to content

Commit

Permalink
Add '--setopt' option for setting config options from the command-line
Browse files Browse the repository at this point in the history
  • Loading branch information
dmach committed Jul 17, 2023
1 parent c22aceb commit 9ec1b24
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
13 changes: 13 additions & 0 deletions osc/commandline.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,13 @@ def init_arguments(self):
metavar="FILE",
help="specify alternate configuration file",
)
self.add_argument(
"--setopt",
metavar="KEY=VALUE",
action="append",
default=[],
help="set a config option for the current program run",
)
self.add_argument(
"--no-keyring",
action="store_true",
Expand All @@ -401,6 +408,11 @@ def post_parse_args(self, args):
# let's leave setting the right value to conf.get_config()
pass

overrides = {}
for i in args.setopt:
key, value = i.split("=")
overrides[key] = value

Check warning on line 414 in osc/commandline.py

View check run for this annotation

Codecov / codecov/patch

osc/commandline.py#L413-L414

Added lines #L413 - L414 were not covered by tests

try:
conf.get_config(
override_apiurl=args.apiurl,
Expand All @@ -412,6 +424,7 @@ def post_parse_args(self, args):
override_post_mortem=args.post_mortem,
override_traceback=args.traceback,
override_verbose=args.verbose,
overrides=overrides,
)
except oscerr.NoConfigfile as e:
print(e.msg, file=sys.stderr)
Expand Down
21 changes: 18 additions & 3 deletions osc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,13 @@ def apply_option_types(config, conffile=""):
cp = OscConfigParser.OscConfigParser(config)
cp.add_section("general")

typed_opts = ((_boolean_opts, cp.getboolean), (_integer_opts, cp.getint))
for opts, meth in typed_opts:
typed_opts = ((_boolean_opts, cp.getboolean, bool), (_integer_opts, cp.getint, int))
for opts, meth, typ in typed_opts:
for opt in opts:
if opt not in config:
continue
if isinstance(config[opt], typ):
continue
try:
config[opt] = meth('general', opt)
except ValueError as e:
Expand Down Expand Up @@ -750,7 +752,9 @@ def get_config(override_conffile=None,
override_traceback=None,
override_post_mortem=None,
override_no_keyring=None,
override_verbose=None):
override_verbose=None,
overrides=None
):
"""do the actual work (see module documentation)"""
global config

Expand Down Expand Up @@ -786,6 +790,17 @@ def get_config(override_conffile=None,
raise oscerr.ConfigError(msg, conffile)

config = dict(cp.items('general', raw=1))

# if the overrides trigger an exception, the 'post_mortem' option
# must be set to the appropriate type otherwise the non-empty string gets evaluated as True
config = apply_option_types(config, conffile)

overrides = overrides or {}
for key, value in overrides.items():
if key not in config:
raise oscerr.ConfigError(f"Unknown config option '{key}'", "<command-line>")
config[key] = value

Check warning on line 802 in osc/conf.py

View check run for this annotation

Codecov / codecov/patch

osc/conf.py#L800-L802

Added lines #L800 - L802 were not covered by tests

config['conffile'] = conffile

config = apply_option_types(config, conffile)
Expand Down

0 comments on commit 9ec1b24

Please sign in to comment.