[conf] halfapi config now dumps the .ini of the current config

This commit is contained in:
Maxime Alves LIRMM 2021-11-23 13:35:02 +01:00
parent f0e662e060
commit e7e1bfed1b
3 changed files with 28 additions and 22 deletions

View File

@ -7,31 +7,25 @@ Defines the "halfapi config" command
import click import click
from .cli import cli from .cli import cli
from ..conf import ( from ..conf import CONFIG
read_config,
PROJECT_NAME, DOMAINS_STR='\n'.join(
DOMAINSDICT, [ ' = '.join((key, val.__name__)) for key, val in CONFIG['domains'].items() ]
HOST,
PORT,
PRODUCTION,
) )
CONF_STR=f""" CONF_STR="""
[project] [project]
name = {PROJECT_NAME} name = {project_name}
host = {HOST} host = {host}
port = {PORT} port = {port}
production = {PRODUCTION} production = {production}
[domains]"""
for dom in DOMAINSDICT():
CONF_STR = '\n'.join((CONF_STR, dom))
[domains]
""".format(**CONFIG) + DOMAINS_STR
@cli.command() @cli.command()
def config(): def config():
""" """
Lists config parameters and their values Lists config parameters and their values
""" """
click.echo(read_config()) click.echo(CONF_STR)

View File

@ -110,19 +110,19 @@ if len(PROJECT_NAME) == 0:
DOMAINSDICT = lambda: d_domains(config) DOMAINSDICT = lambda: d_domains(config)
DOMAINS = DOMAINSDICT() DOMAINS = DOMAINSDICT()
if len(DOMAINS) == 0: 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')) 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')) PORT = config.getint('project', 'port', fallback=environ.get('HALFAPI_PORT', '3000'))
secret_path = config.get('project', 'secret', fallback=environ.get('HALFAPI_SECRET', ''))
try: try:
with open(config.get('project', 'secret', with open(secret_path, 'r') as secret_file:
fallback=environ.get('HALFAPI_SECRET', ''))) as secret_file:
SECRET = secret_file.read().strip() SECRET = secret_file.read().strip()
CONFIG['secret'] = SECRET.strip() CONFIG['secret'] = SECRET.strip()
except FileNotFoundError as exc: 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', PRODUCTION = config.getboolean('project', 'production',
fallback=environ.get('HALFAPI_PROD', True)) fallback=environ.get('HALFAPI_PROD', True))
@ -137,6 +137,8 @@ CONFIG = {
'project_name': PROJECT_NAME, 'project_name': PROJECT_NAME,
'production': PRODUCTION, 'production': PRODUCTION,
'secret': SECRET, 'secret': SECRET,
'host': HOST,
'port': PORT,
'domains': DOMAINS, 'domains': DOMAINS,
'domain_config': {} 'domain_config': {}
} }

View File

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