diff --git a/pmb/config/__init__.py b/pmb/config/__init__.py index b83c7de4..aa683813 100644 --- a/pmb/config/__init__.py +++ b/pmb/config/__init__.py @@ -9,7 +9,7 @@ from typing import List # # Exported functions # -from pmb.config.load import load +from pmb.config.load import load, sanity_checks from pmb.config.save import save from pmb.config.merge_with_args import merge_with_args from pmb.config.sudo import which_sudo @@ -149,6 +149,9 @@ defaults = { "log": "$WORK/log.txt", } +allowed_values = { + "systemd": ["default", "always", "never"], +} # Whether we're connected to a TTY (which allows things like e.g. printing # progress bars) diff --git a/pmb/config/init.py b/pmb/config/init.py index d80c1d2d..46a615ac 100644 --- a/pmb/config/init.py +++ b/pmb/config/init.py @@ -203,7 +203,7 @@ def ask_for_systemd(args, ui): logging.info("Based on your UI selection, 'default' will result" f" in{not_str}installing systemd.") - choices = ["default", "always", "never"] + choices = pmb.config.allowed_values["systemd"] answer = pmb.helpers.cli.ask("Install systemd?", choices, args.systemd, diff --git a/pmb/config/load.py b/pmb/config/load.py index f67215f3..eca8b346 100644 --- a/pmb/config/load.py +++ b/pmb/config/load.py @@ -3,9 +3,30 @@ import logging import configparser import os +import sys import pmb.config +def sanity_check(args, cfg, key, allowed, print_path): + value = cfg["pmbootstrap"][key] + + if value in allowed: + return + + logging.error(f"pmbootstrap.cfg: invalid value for {key}: '{value}'") + logging.error(f"Allowed: {', '.join(allowed)}") + + if print_path: + logging.error(f"Fix it here and try again: {args.config}") + + sys.exit(1) + + +def sanity_checks(args, cfg, print_path=True): + for key, allowed in pmb.config.allowed_values.items(): + sanity_check(args, cfg, key, allowed, print_path) + + def load(args): cfg = configparser.ConfigParser() if os.path.isfile(args.config): @@ -31,4 +52,6 @@ def load(args): f" {cfg['pmbootstrap'][key]}") del cfg["pmbootstrap"][key] + sanity_checks(args, cfg) + return cfg diff --git a/pmb/helpers/frontend.py b/pmb/helpers/frontend.py index 7d85ff20..a663ac0f 100644 --- a/pmb/helpers/frontend.py +++ b/pmb/helpers/frontend.py @@ -213,6 +213,7 @@ def config(args): pmb.config.save(args, cfg) elif args.value is not None: cfg["pmbootstrap"][args.name] = args.value + pmb.config.sanity_checks(args, cfg, False) logging.info("Config changed: " + args.name + "='" + args.value + "'") pmb.config.save(args, cfg) elif args.name: