From ab804354de9c9a6ef826eb4d7a0aa2daa3df4fc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20B=C3=BChler?= Date: Wed, 15 Nov 2023 09:20:36 +0100 Subject: [PATCH] add --config CLI parameter --- src/capport/api/hypercorn_run.py | 18 +++++++++++++++++- src/capport/config.py | 10 ++++++++-- src/capport/control/run.py | 17 ++++++++++++++++- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/capport/api/hypercorn_run.py b/src/capport/api/hypercorn_run.py index c922fff..ac1e94c 100644 --- a/src/capport/api/hypercorn_run.py +++ b/src/capport/api/hypercorn_run.py @@ -1,5 +1,8 @@ from __future__ import annotations +import argparse +import dataclasses + import hypercorn.config import hypercorn.trio.run import hypercorn.utils @@ -19,8 +22,21 @@ def run(config: hypercorn.config.Config) -> None: sock.close() +@dataclasses.dataclass +class CliArguments: + config: str | None + + def __init__(self): + parser = argparse.ArgumentParser() + parser.add_argument('--config', '-c') + args = parser.parse_args() + + self.config = args.config + + def main() -> None: - _config = capport.config.Config.load_default_once() + args = CliArguments() + _config = capport.config.Config.load_default_once(filename=args.config) hypercorn_config = hypercorn.config.Config() hypercorn_config.application_path = 'capport.api.app' diff --git a/src/capport/config.py b/src/capport/config.py index 3317115..d897f24 100644 --- a/src/capport/config.py +++ b/src/capport/config.py @@ -12,6 +12,7 @@ _cached_config: typing.Optional[Config] = None @dataclasses.dataclass class Config: + _source_filename: str controllers: typing.List[str] server_names: typing.List[str] comm_secret: str @@ -21,10 +22,14 @@ class Config: debug: bool @staticmethod - def load_default_once() -> Config: + def load_default_once(filename: typing.Optional[str] = None) -> Config: global _cached_config if not _cached_config: - _cached_config = Config.load() + _cached_config = Config.load(filename) + elif not filename is None and filename != _cached_config._source_filename: + raise RuntimeError( + f"Already loaded different config from {_cached_config._source_filename!r} instead of {filename!r}", + ) return _cached_config @staticmethod @@ -38,6 +43,7 @@ class Config: data = yaml.safe_load(f) controllers = list(map(str, data['controllers'])) return Config( + _source_filename=filename, controllers=controllers, server_names=data.get('server-names', []), comm_secret=str(data.get('comm-secret', None) or data['secret']), diff --git a/src/capport/control/run.py b/src/capport/control/run.py index da042df..c4c5300 100644 --- a/src/capport/control/run.py +++ b/src/capport/control/run.py @@ -1,5 +1,7 @@ from __future__ import annotations +import argparse +import dataclasses import typing import uuid @@ -61,8 +63,21 @@ async def amain(config: capport.config.Config) -> None: await sn.send('STATUS=Kernel fully synchronized') +@dataclasses.dataclass +class CliArguments: + config: str | None + + def __init__(self): + parser = argparse.ArgumentParser() + parser.add_argument('--config', '-c') + args = parser.parse_args() + + self.config = args.config + + def main() -> None: - config = capport.config.Config.load_default_once() + args = CliArguments() + config = capport.config.Config.load_default_once(filename=args.config) capport.utils.cli.init_logger(config) try: trio.run(amain, config)