[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
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)

View File

@ -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': {}
}

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