More command line stuff

This commit is contained in:
Raoul Snyman 2021-05-30 23:31:51 -07:00
parent 9f375207d9
commit a966f018f7
No known key found for this signature in database
GPG Key ID: 423F9B322D9BEF2C

View File

@ -24,6 +24,17 @@ The :mod:`~scribeengine.config` module contains helper classes for config handli
""" """
from argparse import ArgumentParser from argparse import ArgumentParser
from configparser import ConfigParser from configparser import ConfigParser
from random import SystemRandom
from string import ascii_letters, digits, punctuation
ALLOWED_CHARS = ascii_letters + digits + punctuation
def _generate_secret(length=32):
"""
Generate a secret
"""
return ''.join(SystemRandom().choice(ALLOWED_CHARS) for _ in range(length))
def parse_args(): def parse_args():
@ -75,15 +86,73 @@ def prompt(question, options=None, default=None, type_=str):
return answer return answer
def ask_db_questions(): def build_db_url(db_type=None, db_name=None, db_user=None, db_password=None, db_host=None, db_port=None):
"""
Build an SQLAlchemy connection string using the values given, using defaults for omitted values
:param db_type: The type of database, "sqlite", "mysql", "postgres"
:param db_name: The name of the database
:param db_user: The user to connect to the database
:param db_password: The password used to authenticate the user
:param db_host: The hostname or IP address of the database server
:param db_port: The port of the database server
;
:return: An SQLAlchemy connection string
"""
if not db_type:
db_type = 'sqlite'
if not db_name:
if db_type == 'sqlite':
return 'sqlite:///scribeengine.sqlite'
db_name = 'scribeengine'
if not db_user:
db_user = 'scribeegine'
if not db_password:
db_password = _generate_secret(40)
if not db_host:
db_host = 'localhost'
if not db_port:
if db_type == 'mysql':
db_port = 3306
elif db_type == 'postgres':
db_port = 5432
else:
db_port = 0
return '{db_type}://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}'.format(db_type=db_type,
db_user=db_user,
db_password=db_password,
db_host=db_host,
db_port=db_port,
db_name=db_name)
def ask_db_questions(db_type=None, db_name=None, db_user=None, db_password=None, db_host=None, db_port=None):
"""
Ask questions about the database in order to build a SQLAlchemy connection string
:param db_type: The type of database, "sqlite", "mysql", "postgres"
:param db_name: The name of the database
:param db_user: The user to connect to the database
:param db_password: The password used to authenticate the user
:param db_host: The hostname or IP address of the database server
:param db_port: The port of the database server
:return: An SQLAlchemy connection string
"""
if not db_type:
db_type = prompt('Database type', ['sqlite', 'mysql', 'postgres'], 'sqlite').lower() db_type = prompt('Database type', ['sqlite', 'mysql', 'postgres'], 'sqlite').lower()
if not db_name:
if db_type == 'sqlite': if db_type == 'sqlite':
db_name = prompt('Database filename', default='scribeengine.sqlite') db_name = prompt('Database filename', default='scribeengine.sqlite')
return '{db_type}:///{db_name}'.format(db_type=db_type, db_name=db_name) return '{db_type}:///{db_name}'.format(db_type=db_type, db_name=db_name)
db_name = prompt('Database name', default='scribeengine') db_name = prompt('Database name', default='scribeengine')
if not db_user:
db_user = prompt('Database user', default='scribeengine') db_user = prompt('Database user', default='scribeengine')
db_password = prompt('Database password', default='mysupersecretpassword') if not db_password:
db_password = prompt('Database password', default=_generate_secret(40))
if not db_host:
db_host = prompt('Database host', default='localhost') db_host = prompt('Database host', default='localhost')
if not db_port:
if db_type in ['mysql', 'mariadb']: if db_type in ['mysql', 'mariadb']:
db_port = prompt('Database port', default=3306, type_=int) db_port = prompt('Database port', default=3306, type_=int)
elif db_type == 'postgres': elif db_type == 'postgres':
@ -103,9 +172,25 @@ def run_cmd():
Ask the user a set of questions, and then write them to a config file. Ask the user a set of questions, and then write them to a config file.
""" """
args = parse_args() args = parse_args()
if args.non_interactive:
db_url = build_db_url(args.db_type, args.db_name, args.db_user, args.db_password, args.db_host, args.db_port)
else:
db_url = ask_db_questions(args.db_type, args.db_name, args.db_user, args.db_password, args.db_host,
args.db_port)
config = { config = {
'flask': {
'secret_key': _generate_secret()
},
'sqlalchemy': { 'sqlalchemy': {
'database_uri': ask_db_questions() 'database_uri': db_url,
'track_modifications': False
},
'mail': {
},
'paths': {
'theme_path',
'uploads_path'
} }
} }
write_config_file(config, args.config or 'config.ini') write_config_file(config, args.config or 'config.ini')