Add sanity check for "systemd" config values (MR 2273)

Make sure users don't set systemd to "true" or other not allowed values
(allowed are "always", "default", "never").

Check for it:
* after loading the config
* when using 'pmbootstrap config systemd <newvalue>'
This commit is contained in:
Oliver Smith 2024-03-11 17:27:12 +01:00
parent de5e4c6962
commit 4478116379
No known key found for this signature in database
GPG Key ID: 5AE7F5513E0885CB
4 changed files with 29 additions and 2 deletions

View File

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

View File

@ -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,

View File

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

View File

@ -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: