1
0
mirror of https://github.com/openSUSE/osc.git synced 2025-01-13 17:16:23 +01:00

Merge pull request #1358 from dmach/setopt

Add '--setopt' option for setting config options from the command-line
This commit is contained in:
Daniel Mach 2023-07-31 13:27:47 +02:00 committed by GitHub
commit 001bbdf365
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 3 deletions

View File

@ -384,6 +384,13 @@ class OscMainCommand(MainCommand):
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",
@ -402,6 +409,11 @@ class OscMainCommand(MainCommand):
# 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
try:
conf.get_config(
override_apiurl=args.apiurl,
@ -413,6 +425,7 @@ class OscMainCommand(MainCommand):
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)

View File

@ -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:
@ -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
@ -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
config['conffile'] = conffile
config = apply_option_types(config, conffile)