From c96640bfd77937231ac1b94c3b6ec17387ac3791 Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Sun, 20 Nov 2022 06:14:37 -0500 Subject: [PATCH] add config cli commands --- relay/config.py | 10 +++++++--- relay/manage.py | 33 +++++++++++++++++++++++++++++++-- relay/misc.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 5 deletions(-) diff --git a/relay/config.py b/relay/config.py index 998e5d6..e4ee5f8 100644 --- a/relay/config.py +++ b/relay/config.py @@ -4,7 +4,7 @@ import yaml from pathlib import Path from urllib.parse import urlparse -from .misc import DotDict +from .misc import DotDict, boolean relay_software_names = [ @@ -51,10 +51,12 @@ class RelayConfig(DotDict): assert isinstance(value, (list, set, tuple)) elif key in ['port', 'workers', 'json', 'objects', 'digests']: - assert isinstance(value, (int)) + if not isinstance(value, int): + value = int(value) elif key == 'whitelist_enabled': - assert isinstance(value, bool) + if not isinstance(value, bool): + value = boolean(value) super().__setitem__(key, value) @@ -216,6 +218,8 @@ class RelayConfig(DotDict): self[k] = v + continue + elif key not in self: continue diff --git a/relay/manage.py b/relay/manage.py index ecd466f..8458fd1 100644 --- a/relay/manage.py +++ b/relay/manage.py @@ -12,6 +12,7 @@ from .config import relay_software_names app = None +CONFIG_IGNORE = {'blocked_software', 'blocked_instances', 'whitelist'} @click.group('cli', context_settings={'show_default': True}, invoke_without_command=True) @@ -30,9 +31,37 @@ def cli(ctx, config): relay_run.callback() -@cli.group('inbox') +# todo: add config default command for resetting config key +@cli.group('config', invoke_without_command=True) @click.pass_context -def cli_inbox(ctx): +def cli_config(ctx): + 'List the current relay config' + + if ctx.invoked_subcommand: + return + + click.echo('Relay Config:') + + for key, value in app.config.items(): + if key not in CONFIG_IGNORE: + key = f'{key}:'.ljust(20) + click.echo(f'- {key} {value}') + + +@cli_config.command('set') +@click.argument('key') +@click.argument('value') +def cli_config_set(key, value): + 'Set a config value' + + app.config[key] = value + app.config.save() + + print(f'{key}: {app.config[key]}') + + +@cli.group('inbox') +def cli_inbox(): 'Manage the inboxes in the database' pass diff --git a/relay/misc.py b/relay/misc.py index 980caf3..e1fe01e 100644 --- a/relay/misc.py +++ b/relay/misc.py @@ -50,6 +50,37 @@ def build_signing_string(headers, used_headers): return '\n'.join(map(lambda x: ': '.join([x.lower(), headers[x]]), used_headers)) +def boolean(value): + if isinstance(value, str): + if value.lower() in ['on', 'y', 'yes', 'true', 'enable', 'enabled', '1']: + return True + + elif value.lower() in ['off', 'n', 'no', 'false', 'disable', 'disable', '0']: + return False + + else: + raise TypeError(f'Cannot parse string "{value}" as a boolean') + + elif isinstance(value, int): + if value == 1: + return True + + elif value == 0: + return False + + else: + raise ValueError('Integer value must be 1 or 0') + + elif value == None: + return False + + try: + return value.__bool__() + + except AttributeError: + raise TypeError(f'Cannot convert object of type "{clsname(value)}"') + + def check_open_port(host, port): if host == '0.0.0.0': host = '127.0.0.1'