diff --git a/halfapi/cli/config.py b/halfapi/cli/config.py index 0d4befe..9fe4f13 100644 --- a/halfapi/cli/config.py +++ b/halfapi/cli/config.py @@ -7,31 +7,25 @@ Defines the "halfapi config" command import click from .cli import cli -from ..conf import ( - read_config, - PROJECT_NAME, - DOMAINSDICT, - HOST, - PORT, - PRODUCTION, +from ..conf import CONFIG + +DOMAINS_STR='\n'.join( + [ ' = '.join((key, val.__name__)) for key, val in CONFIG['domains'].items() ] ) -CONF_STR=f""" +CONF_STR=""" [project] -name = {PROJECT_NAME} -host = {HOST} -port = {PORT} -production = {PRODUCTION} - -[domains]""" - -for dom in DOMAINSDICT(): - CONF_STR = '\n'.join((CONF_STR, dom)) +name = {project_name} +host = {host} +port = {port} +production = {production} +[domains] +""".format(**CONFIG) + DOMAINS_STR @cli.command() def config(): """ Lists config parameters and their values """ - click.echo(read_config()) + click.echo(CONF_STR) diff --git a/halfapi/conf.py b/halfapi/conf.py index 4753247..58f754b 100644 --- a/halfapi/conf.py +++ b/halfapi/conf.py @@ -110,19 +110,19 @@ if len(PROJECT_NAME) == 0: DOMAINSDICT = lambda: d_domains(config) DOMAINS = DOMAINSDICT() if len(DOMAINS) == 0: - logger.info('Domain-less instance %s', d_domains(config)) + logger.info('Running without domains: %s', d_domains(config) or 'empty domain dictionary') HOST = config.get('project', 'host', fallback=environ.get('HALFAPI_HOST', '127.0.0.1')) PORT = config.getint('project', 'port', fallback=environ.get('HALFAPI_PORT', '3000')) +secret_path = config.get('project', 'secret', fallback=environ.get('HALFAPI_SECRET', '')) try: - with open(config.get('project', 'secret', - fallback=environ.get('HALFAPI_SECRET', ''))) as secret_file: + with open(secret_path, 'r') as secret_file: SECRET = secret_file.read().strip() CONFIG['secret'] = SECRET.strip() except FileNotFoundError as exc: - logger.error('Missing secret file: %s', exc) + logger.info('Running without secret file: %s', secret_path or 'no file specified') PRODUCTION = config.getboolean('project', 'production', fallback=environ.get('HALFAPI_PROD', True)) @@ -137,6 +137,8 @@ CONFIG = { 'project_name': PROJECT_NAME, 'production': PRODUCTION, 'secret': SECRET, + 'host': HOST, + 'port': PORT, 'domains': DOMAINS, 'domain_config': {} } diff --git a/tests/cli/test_cli_config.py b/tests/cli/test_cli_config.py new file mode 100644 index 0000000..e79c6a9 --- /dev/null +++ b/tests/cli/test_cli_config.py @@ -0,0 +1,10 @@ +from halfapi.cli.cli import cli +from configparser import ConfigParser + +def test_config(cli_runner): + with cli_runner.isolated_filesystem(): + result = cli_runner.invoke(cli, ['config']) + cp = ConfigParser() + cp.read_string(result.output) + assert cp.has_section('project') + assert cp.has_section('domains')