Skip to content
Snippets Groups Projects

cli: add --config-file option to 'swh web' group

Compare and
2 files
+ 50
8
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 46
4
@@ -3,7 +3,8 @@
# License: GNU General Public License version 3, or any later version
# See top-level LICENSE file for more information
from typing import List
import os
from typing import Any, Dict, List
# WARNING: do not import unnecessary things here to keep cli startup time under
# control
@@ -14,17 +15,58 @@ from swh.core.cli import swh as swh_cli_group
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
# TODO (T1410): All generic config code should reside in swh.core.config
DEFAULT_CONFIG_PATH = os.environ.get(
"SWH_CONFIG_FILE", os.path.join(click.get_app_dir("swh"), "global.yml")
)
DEFAULT_CONFIG: Dict[str, Any] = {
"api_url": "https://archive.softwareheritage.org/api/1",
"bearer_token": None,
}
@swh_cli_group.group(name="web", context_settings=CONTEXT_SETTINGS)
@click.option(
"-C",
"--config-file",
default=None,
type=click.Path(exists=True, dir_okay=False, path_type=str),
help=f"Configuration file (default: {DEFAULT_CONFIG_PATH})",
)
@click.pass_context
def web(ctx: Context):
def web(ctx: Context, config_file: str):
"""Software Heritage web client"""
import logging
from swh.core import config
from swh.web.client.client import WebAPIClient
if not config_file:
config_file = DEFAULT_CONFIG_PATH
try:
conf = config.read_raw_config(config.config_basepath(config_file))
if not conf:
raise ValueError(f"Cannot parse configuration file: {config_file}")
if config_file == DEFAULT_CONFIG_PATH:
try:
conf = conf["swh"]["web"]["client"]
except KeyError:
pass
# recursive merge not done by config.read
conf = config.merge_configs(DEFAULT_CONFIG, conf)
except Exception:
logging.warning(
"Using default configuration (cannot load custom one)", exc_info=True
)
conf = DEFAULT_CONFIG
ctx.ensure_object(dict)
# TODO (T2872): add configuration file for the web client
ctx.obj["client"] = WebAPIClient()
ctx.obj["client"] = WebAPIClient(conf["api_url"], conf["bearer_token"])
@web.command(name="search")
Loading