]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Fail when CLI configuration is not applied to any image
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 27 Oct 2023 13:41:41 +0000 (15:41 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 27 Oct 2023 15:30:58 +0000 (17:30 +0200)
Currently we silently ignore settings configured via the CLI that
are overridden by image configuration files. Let's instead fail with
a descriptive error message when this happens, indicating users that
they should use '@' in their configuration files to set the default
value for the setting if they want to allow overriding it from the
command line.

mkosi/config.py
tests/test_config.py

index 6f3320b23432d2ff576d1f0c85862abd54349099..739d206d02ea62fbf3ee60e72183341fc7ada971 100644 (file)
@@ -2386,6 +2386,7 @@ def parse_config(argv: Sequence[str] = ()) -> tuple[MkosiArgs, tuple[MkosiConfig
     namespace = argparse.Namespace()
     argparser = create_argument_parser(MkosiAction)
     argparser.parse_args(argv, namespace)
+    cli_ns = copy.deepcopy(namespace)
 
     args = load_args(namespace)
 
@@ -2437,6 +2438,24 @@ def parse_config(argv: Sequence[str] = ()) -> tuple[MkosiArgs, tuple[MkosiConfig
         finalize_defaults(namespace, defaults)
         images = [namespace]
 
+    for s in vars(cli_ns):
+        if s not in SETTINGS_LOOKUP_BY_DEST:
+            continue
+
+        if getattr(cli_ns, s) is None:
+            continue
+
+        if isinstance(getattr(cli_ns, s), (list, tuple)):
+            continue
+
+        if any(getattr(config, s) == getattr(cli_ns, s) for config in images):
+            continue
+
+        setting = SETTINGS_LOOKUP_BY_DEST[s]
+
+        die(f"{setting.long}={getattr(cli_ns, s)} was specified on the command line but is not allowed to be configured by any images.",
+            hint="Prefix the setting with '@' in the image configuration file to allow overriding it from the command line.")
+
     if not images:
         die("No images defined in mkosi.images/")
 
index ffa9625c4adc60f720a05f22f609866b919dc8ea..f721e9688ac845c41c580c0937301e877f26d87e 100644 (file)
@@ -114,12 +114,14 @@ def test_parse_config(tmp_path: Path) -> None:
     assert config.image_id == "base"
 
     with chdir(d):
-        _, [config] = parse_config(["--distribution", "fedora", "--architecture", "x86-64"])
+        _, [config] = parse_config(["--distribution", "fedora"])
 
     # mkosi.conf sets a default distribution, so the CLI should take priority.
     assert config.distribution == Distribution.fedora
-    # mkosi.conf sets overrides the architecture, so whatever is specified on the CLI should be ignored.
-    assert config.architecture == Architecture.arm64
+
+    # Any architecture set on the CLI is overridden by the config file, and we should complain loudly about that.
+    with chdir(d), pytest.raises(SystemExit):
+        _, [config] = parse_config(["--architecture", "x86-64"])
 
     (d / "mkosi.conf.d").mkdir()
     (d / "mkosi.conf.d/d1.conf").write_text(