From 261dce50ab378ab008f89824b6f997001401163d Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Tue, 20 Dec 2022 07:59:58 -0500 Subject: [PATCH] let setup command configure the database --- relay/config.py | 4 ++++ relay/manage.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/relay/config.py b/relay/config.py index 7e9f409..885c86d 100644 --- a/relay/config.py +++ b/relay/config.py @@ -32,6 +32,7 @@ DEFAULTS = { CATEGORY_NAMES = [ 'general', 'database', + 'sqlite', 'postgres', 'mysql' ] @@ -85,6 +86,9 @@ class Config(AppBase, dict): def __setitem__(self, key, value): + if key in {'database', 'hostname', 'port', 'username', 'password'}: + key = f'{self.dbtype}_{key}' + if (self.is_docker and key in {'general_host', 'general_port'}) or value == '__DEFAULT__': value = DEFAULTS[key] diff --git a/relay/manage.py b/relay/manage.py index 5946f03..5d43d60 100644 --- a/relay/manage.py +++ b/relay/manage.py @@ -45,6 +45,8 @@ def cli(ctx, config): @cli.command('convert') @click.option('--old-config', '-o', help='path to the old relay config') def cli_convert(old_config): + 'Convert an old relay.yaml and relay.jsonld to the the new formats' + with open(old_config or 'relay.yaml') as fd: config = yaml.load(fd.read(), Loader=yaml.SafeLoader) ap = config.get('ap', {}) @@ -106,7 +108,10 @@ def cli_setup(): 'Generate a new config' while True: - app.config['general_host'] = click.prompt('What domain will the relay be hosted on?', default=app.config.host) + app.config['general_host'] = click.prompt( + 'What domain will the relay be hosted on?', + default = app.config.host + ) if not app.config.host.endswith('example.com'): break @@ -114,12 +119,60 @@ def cli_setup(): click.echo('The domain must not be example.com') if not app.config.is_docker: - app.config['general_listen'] = click.prompt('Which address should the relay listen on?', default=app.config.listen) + app.config['general_listen'] = click.prompt( + 'Which address should the relay listen on?', + default = app.config.listen + ) while True: - app.config['general_port'] = click.prompt('What TCP port should the relay listen on?', default=app.config.port, type=int) + app.config['general_port'] = click.prompt( + 'What TCP port should the relay listen on?', + default = app.config.port, + type = int + ) + break + app.config['database_type'] = click.prompt( + 'What database backend would you like to use for the relay?', + default = app.config.dbtype, + type = click.Choice(['sqlite', 'postgresql', 'mysql']), + show_choices = True + ) + + if app.config.dbtype == 'sqlite': + app.config['sqlite_database'] = click.prompt( + 'Where would you like to store your database file? Relative paths are relative to the config file location.', + default = app.config['sqlite_database'] + ) + + else: + dbconfig = app.config.dbconfig + app.config.hostname = click.prompt( + 'What address is your database listening on?', + default = dbconfig.hostname + ) or None + + app.config.port = click.prompt( + 'What port is your database listening on?', + default = dbconfig.port + ) or None + + app.config.database = click.prompt( + 'What would you like the name of the database be?', + default = dbconfig.database + ) or None + + app.config.username = click.prompt( + 'Which user will be connecting to the database?', + default = dbconfig.username + ) or None + + app.config.password = click.prompt( + 'What is the database user\'s password?', + default = dbconfig.password + ) or None + app.config.save() with app.database.session as s: