refactor: slightly clean up config parsing

This commit is contained in:
ptrcnull 2024-07-27 16:23:47 +02:00
parent 5fad1f8ebe
commit c37753e0cf

View file

@ -1,7 +1,7 @@
import logging import logging
import os import os
import sys import sys
from typing import Optional from typing import Any, Optional
import tomllib import tomllib
@ -33,6 +33,20 @@ class Config:
return self.secrets[handler] return self.secrets[handler]
def config_parse_dict(raw_conf: dict[str, Any], key: str) -> dict[str, str]:
if key not in raw_conf:
log.error(f'missing "{key}" in config')
sys.exit(1)
for k, v in raw_conf[key].items():
assert isinstance(k, str), f'"{k}" is not a string'
assert isinstance(v, str), f'"{k}" value "{v}" is not a string'
result: dict[str, str] = raw_conf[key]
return result
def read_config(path: Optional[str]) -> Config: def read_config(path: Optional[str]) -> Config:
if not path: if not path:
# should be here only when running from hook # should be here only when running from hook
@ -47,25 +61,8 @@ def read_config(path: Optional[str]) -> Config:
c = Config() c = Config()
if 'domains' not in raw_conf: c.domains = config_parse_dict(raw_conf, 'domains')
log.error('missing "domains"') c.secrets = config_parse_dict(raw_conf, 'secrets')
sys.exit(1)
for k, v in raw_conf['domains'].items():
assert isinstance(k, str), f'domain "{k}" is not a string'
assert isinstance(v, str), f'domain "{k}" handler {v} is not a string'
c.domains = raw_conf['domains']
if 'secrets' not in raw_conf:
log.error('missing "secrets"')
sys.exit(1)
for k, v in raw_conf['secrets'].items():
assert isinstance(k, str), f'secret key "{k}" is not a string'
assert isinstance(v, str), f'secret "{k}" value {v} is not a string'
c.secrets = raw_conf['secrets']
post_acquire = [] post_acquire = []
if 'post_acquire' in raw_conf: if 'post_acquire' in raw_conf: