1
0
mirror of https://github.com/openSUSE/osc.git synced 2025-09-06 13:18:42 +02:00

Move code setting apiurl from store to 'osc.conf.get_config()'

This commit is contained in:
2025-07-31 11:13:46 +02:00
parent a1db424241
commit e08644ea4e
2 changed files with 34 additions and 30 deletions

View File

@@ -146,20 +146,6 @@ class OscMainCommand(MainCommand):
# HACK: never ask for credentials when displaying help
return
if args.apiurl is None:
# apiurl hasn't been specified by the user
# we need to set it here because the 'default' option of an argument doesn't support lazy evaluation
try:
# try reading the apiurl from the working copy
args.apiurl = osc_store.get_store(Path.cwd()).apiurl
except oscerr.NoWorkingCopy:
# we can't use conf.config["apiurl"] because it contains the default "https://api.opensuse.org"
# let's leave setting the right value to conf.get_config()
pass
else:
# apiurl has been specified by the user, let's ignore OSC_APIURL env
os.environ.pop("OSC_APIURL", None)
overrides = {}
for i in args.setopt:
key, value = i.split("=")
@@ -178,6 +164,7 @@ class OscMainCommand(MainCommand):
override_traceback=args.traceback,
override_verbose=args.verbose,
overrides=overrides,
store_dir=Path.cwd(),
)
except oscerr.NoConfigfile as e:
print(e.msg, file=sys.stderr)

View File

@@ -1835,15 +1835,17 @@ def get_config(override_conffile=None,
override_quiet=None,
override_no_keyring=None,
override_verbose=None,
overrides=None
overrides=None,
store_dir: Optional[str] = None,
):
"""
Configure osc.
The configuration options are loaded with the following priority:
1. environment variables: ``OSC_<uppercase_option>`` or ``OSC_<uppercase_host_alias>_<uppercase_host_option>``
2. override arguments provided to ``get_config()``
3. oscrc config file
1. override arguments provided to ``get_config()``, they frequently come from the command-line
2. values from store (apiurl)
3. environment variables: ``OSC_<uppercase_option>`` or ``OSC_<uppercase_host_alias>_<uppercase_host_option>``
4. oscrc config file
"""
if overrides:
@@ -2009,33 +2011,48 @@ def get_config(override_conffile=None,
known_ini_keys.add(name)
env_key = f"OSC_{name.upper()}"
# priority: env, overrides, config
if env_key in os.environ:
value = os.environ[env_key]
# remove any matching records from overrides because they are checked for emptiness later
overrides.pop(name, None)
overrides.pop(ini_key, None)
elif name in overrides:
# priority: overrides (frequently come from the command-line), store, env, config
read_value_from_store = False
if name in overrides:
value = overrides.pop(name)
elif ini_key in overrides:
value = overrides.pop(ini_key)
elif env_key in os.environ:
value = os.environ[env_key]
read_value_from_store = True
elif ini_key in cp["general"]:
value = cp["general"][ini_key]
read_value_from_store = True
else:
continue
if name == "apiurl":
# resolve an apiurl alias to an actual apiurl
apiurl = config.apiurl_aliases.get(value, None)
def resolve_apiurl_alias(alias: str) -> str:
apiurl = config.apiurl_aliases.get(alias, None)
if not apiurl:
# no alias matched, try again with a sanitized apiurl (with https:// prefix)
# and if there's no match again, just use the sanitized apiurl
apiurl = sanitize_apiurl(value)
apiurl = sanitize_apiurl(alias)
apiurl = config.apiurl_aliases.get(apiurl, apiurl)
value = apiurl
return apiurl
if name == "apiurl":
value = resolve_apiurl_alias(value)
config.set_value_from_string(name, value)
if name == "apiurl" and read_value_from_store and store_dir:
from . import store as osc_store
# use store.apiurl only if apiurl is set from env or config file
try:
store = osc_store.get_store(store_dir)
if store.apiurl:
value = store.apiurl
value = resolve_apiurl_alias(value)
config.set_value_from_string(name, value)
except oscerr.NoWorkingCopy:
pass
# BEGIN: override credentials for the default apiurl
# OSC_APIURL is handled already because it's a regular field