diff --git a/swh/objstorage/api/server.py b/swh/objstorage/api/server.py --- a/swh/objstorage/api/server.py +++ b/swh/objstorage/api/server.py @@ -206,8 +206,6 @@ Args: config_file (str): Path to the configuration file to load - type (str): configuration type. For 'local' type, more - checks are done. Raises: Error if the setup is not as expected @@ -223,7 +221,23 @@ raise FileNotFoundError("Configuration file %s does not exist" % (config_file,)) cfg = config_read(config_file) + return validate_config(cfg) + +def validate_config(cfg): + """Check the minimal configuration is set to run the api or raise an + error explanation. + + Args: + cfg (dict): Loaded configuration. + + Raises: + Error if the setup is not as expected + + Returns: + configuration as a dict + + """ if "objstorage" not in cfg: raise KeyError("Invalid configuration; missing objstorage config entry") diff --git a/swh/objstorage/cli.py b/swh/objstorage/cli.py --- a/swh/objstorage/cli.py +++ b/swh/objstorage/cli.py @@ -10,10 +10,11 @@ import click import aiohttp.web +from swh.core import config from swh.core.cli import CONTEXT_SETTINGS -from swh.objstorage.api.server import load_and_check_config, make_app from swh.objstorage.factory import get_objstorage +from swh.objstorage.api.server import validate_config, make_app @click.group(name="objstorage", context_settings=CONTEXT_SETTINGS) @@ -28,9 +29,19 @@ def cli(ctx, config_file): """Software Heritage Objstorage tools. """ + if not config_file: + config_file = os.environ.get("SWH_CONFIG_FILENAME") + + if config_file: + if not os.path.exists(config_file): + raise ValueError("%s does not exist" % config_file) + conf = config.read(config_file) + else: + conf = {} + ctx.ensure_object(dict) - cfg = load_and_check_config(config_file) - ctx.obj["config"] = cfg + + ctx.obj["config"] = conf @cli.command("rpc-serve") @@ -56,7 +67,7 @@ This is not meant to be run on production systems. """ - app = make_app(ctx.obj["config"]) + app = make_app(validate_config(ctx.obj["config"])) if ctx.obj["log_level"] == "DEBUG": app.update(debug=True) aiohttp.web.run_app(app, host=host, port=int(port))