add config cli commands

This commit is contained in:
Izalia Mae 2022-11-20 06:14:37 -05:00
parent 9839da906c
commit c96640bfd7
3 changed files with 69 additions and 5 deletions

View file

@ -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

View file

@ -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

View file

@ -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'