mirror of
https://github.com/openSUSE/osc.git
synced 2025-01-27 23:16:14 +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:
commit
001bbdf365
@ -384,6 +384,13 @@ class OscMainCommand(MainCommand):
|
|||||||
metavar="FILE",
|
metavar="FILE",
|
||||||
help="specify alternate configuration 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(
|
self.add_argument(
|
||||||
"--no-keyring",
|
"--no-keyring",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
@ -402,6 +409,11 @@ class OscMainCommand(MainCommand):
|
|||||||
# let's leave setting the right value to conf.get_config()
|
# let's leave setting the right value to conf.get_config()
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
overrides = {}
|
||||||
|
for i in args.setopt:
|
||||||
|
key, value = i.split("=")
|
||||||
|
overrides[key] = value
|
||||||
|
|
||||||
try:
|
try:
|
||||||
conf.get_config(
|
conf.get_config(
|
||||||
override_apiurl=args.apiurl,
|
override_apiurl=args.apiurl,
|
||||||
@ -413,6 +425,7 @@ class OscMainCommand(MainCommand):
|
|||||||
override_post_mortem=args.post_mortem,
|
override_post_mortem=args.post_mortem,
|
||||||
override_traceback=args.traceback,
|
override_traceback=args.traceback,
|
||||||
override_verbose=args.verbose,
|
override_verbose=args.verbose,
|
||||||
|
overrides=overrides,
|
||||||
)
|
)
|
||||||
except oscerr.NoConfigfile as e:
|
except oscerr.NoConfigfile as e:
|
||||||
print(e.msg, file=sys.stderr)
|
print(e.msg, file=sys.stderr)
|
||||||
|
21
osc/conf.py
21
osc/conf.py
@ -232,11 +232,13 @@ def apply_option_types(config, conffile=""):
|
|||||||
cp = OscConfigParser.OscConfigParser(config)
|
cp = OscConfigParser.OscConfigParser(config)
|
||||||
cp.add_section("general")
|
cp.add_section("general")
|
||||||
|
|
||||||
typed_opts = ((_boolean_opts, cp.getboolean), (_integer_opts, cp.getint))
|
typed_opts = ((_boolean_opts, cp.getboolean, bool), (_integer_opts, cp.getint, int))
|
||||||
for opts, meth in typed_opts:
|
for opts, meth, typ in typed_opts:
|
||||||
for opt in opts:
|
for opt in opts:
|
||||||
if opt not in config:
|
if opt not in config:
|
||||||
continue
|
continue
|
||||||
|
if isinstance(config[opt], typ):
|
||||||
|
continue
|
||||||
try:
|
try:
|
||||||
config[opt] = meth('general', opt)
|
config[opt] = meth('general', opt)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
@ -750,7 +752,9 @@ def get_config(override_conffile=None,
|
|||||||
override_traceback=None,
|
override_traceback=None,
|
||||||
override_post_mortem=None,
|
override_post_mortem=None,
|
||||||
override_no_keyring=None,
|
override_no_keyring=None,
|
||||||
override_verbose=None):
|
override_verbose=None,
|
||||||
|
overrides=None
|
||||||
|
):
|
||||||
"""do the actual work (see module documentation)"""
|
"""do the actual work (see module documentation)"""
|
||||||
global config
|
global config
|
||||||
|
|
||||||
@ -786,6 +790,17 @@ def get_config(override_conffile=None,
|
|||||||
raise oscerr.ConfigError(msg, conffile)
|
raise oscerr.ConfigError(msg, conffile)
|
||||||
|
|
||||||
config = dict(cp.items('general', raw=1))
|
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['conffile'] = conffile
|
||||||
|
|
||||||
config = apply_option_types(config, conffile)
|
config = apply_option_types(config, conffile)
|
||||||
|
Loading…
Reference in New Issue
Block a user