Commit 98904594 authored by Benedikt Neuffer's avatar Benedikt Neuffer
Browse files

Separate base_url and endpoint

This should fix #1.
parent 27141766
[DEFAULT] [DEFAULT]
endpoint = www-net.scc.kit.edu endpoint = prod
[www-net-devel.scc.kit.edu] [dev]
base_url = www-net-devel.scc.kit.edu
version = 3.0 version = 3.0
token = <token> token = <token>
[www-net-test.scc.kit.edu] [test]
base_url = www-net-test.scc.kit.edu
version = 3.0 version = 3.0
token = <token> token = <token>
[www-net.scc.kit.edu] [prod]
base_url = www-net.scc.kit.edu
version = 3.0 version = 3.0
token = <token> token = <token>
...@@ -25,10 +25,14 @@ class ArgumentParser(argparse.ArgumentParser): ...@@ -25,10 +25,14 @@ class ArgumentParser(argparse.ArgumentParser):
self.add_argument('--auth-config', self.add_argument('--auth-config',
default=os.path.expanduser('~/.config/netdb_client.ini'), default=os.path.expanduser('~/.config/netdb_client.ini'),
help='config file path (default: %(default)s)') help='config file path (default: %(default)s)')
self.add_argument('--endpoint', '-e',
help='endpoint to use.\n'
'Environment: "NETDB_ENDPOINT"\n'
'Config: [DEFAULT]: endpoint')
self.add_argument('--base-url', '-b', self.add_argument('--base-url', '-b',
help='webapi server.\n' help='webapi server.\n'
'Environment: "NETDB_BASE_URL"\n' 'Environment: "NETDB_BASE_URL"\n'
'Config: [DEFAULT]: endpoint') 'Config: [$endpoint]: base_url')
self.add_argument('--version', self.add_argument('--version',
help='webapi version.\n' help='webapi version.\n'
'Environment: "NETDB_VERSION"\n' 'Environment: "NETDB_VERSION"\n'
...@@ -47,23 +51,42 @@ class ArgumentParser(argparse.ArgumentParser): ...@@ -47,23 +51,42 @@ class ArgumentParser(argparse.ArgumentParser):
self.error(f"Config file is readable by others. Please set it at least to 660.") self.error(f"Config file is readable by others. Please set it at least to 660.")
config.read(configpath) config.read(configpath)
def load_configoption(option): def load_config(option):
if getattr(args, option) is None: """load config for an option"""
setattr(args, option, os.getenv(f'NETDB_{option.upper()}', None))
if getattr(args, option) is None: # if value is already set, we can stop here
if option == 'base_url': if getattr(args, option) is not None:
setattr(args, option, config.get('DEFAULT', 'endpoint', fallback=None)) return
else:
setattr(args, option, config.get(args.base_url, option, fallback=None)) # try to load option from environment
if getattr(args, option) is None: value = os.getenv(f'NETDB_{option.upper()}', None)
self.error(
f'No {option} specified (looked in args, environment variable ' if value is None:
f'"NETDB_{option.upper()}", config-file in "{args.auth_config}")' # endpoint should be loaded from DEFAULT section
) if option == 'endpoint':
section = 'DEFAULT'
# load base_url first! # everything else loads from the $endpoint section
for configoption in ['base_url', 'token', 'version']: else:
load_configoption(configoption) section = getattr(args, 'endpoint')
# load value from config section
value = config.get(section, option, fallback=None)
# For backwards compatibility:
# if base_url is still unknown use endpoint instead
if option == 'base_url' and value is None:
value = section
if value is None:
self.error(
f'No {option} specified (looked in args, environment variable '
f'"NETDB_{option.upper()}", config-file in "{args.auth_config} section {section}")')
# we have a value now and can use it
setattr(args, option, value)
# start with endpoint to get the right config section
for option in ['endpoint', 'base_url', 'token', 'version']:
load_config(option)
return args return args
......
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