]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Add --json option 2001/head
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 20 Oct 2023 13:52:00 +0000 (15:52 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 20 Oct 2023 14:48:02 +0000 (16:48 +0200)
Let's allow using --json with summary to get a JSON dump of the
configuration.

mkosi/__init__.py
mkosi/config.py
mkosi/resources/mkosi.md
tests/test_json.py

index be1a05ba35d43f8ac2db952986c21edaaf26a2bf..7989e46c4ed39c5a12d0832110f725d7a773cf8b 100644 (file)
@@ -31,6 +31,7 @@ from mkosi.config import (
     ManifestFormat,
     MkosiArgs,
     MkosiConfig,
+    MkosiJsonEncoder,
     OutputFormat,
     SecureBootSignTool,
     Verb,
@@ -2525,10 +2526,15 @@ def run_verb(args: MkosiArgs, presets: Sequence[MkosiConfig]) -> None:
         return bump_image_version()
 
     if args.verb == Verb.summary:
-        text = ""
-
-        for config in presets:
-            text += f"{summary(config)}\n"
+        if args.json:
+            text = json.dumps(
+                {"Presets": [p.to_dict() for p in presets]},
+                cls=MkosiJsonEncoder,
+                indent=4,
+                sort_keys=True
+            )
+        else:
+            text = "\n".join(summary(p) for p in presets)
 
         page(text, args.pager)
         return
index ab2207e0d7b4a76889afe25a9118872707661e93..fb92d5bdb831a1feca3a943c0e40f2730241c0c2 100644 (file)
@@ -628,6 +628,7 @@ class MkosiArgs:
     genkey_common_name: str
     auto_bump: bool
     doc_format: DocFormat
+    json: bool
 
     @classmethod
     def default(cls) -> "MkosiArgs":
@@ -1995,6 +1996,12 @@ def create_argument_parser(action: type[argparse.Action]) -> argparse.ArgumentPa
         default=DocFormat.auto,
         type=DocFormat,
     )
+    parser.add_argument(
+        "--json",
+        help="Show summary as JSON",
+        action="store_true",
+        default=False,
+    )
     # These can be removed once mkosi v15 is available in LTS distros and compatibility with <= v14
     # is no longer needed in build infrastructure (e.g.: OBS).
     parser.add_argument(
index 6dac4b24961a7fe2ab14ca081490d42300e1c666..9f0020e3ccd2c60fdaa325b0f344b805e181b88d 100644 (file)
@@ -215,6 +215,10 @@ Those settings cannot be configured in the configuration files.
   the default, will try all methods in the order `man`, `pandoc`, `markdown`,
   `system`.
 
+`--json`
+
+: Show the summary output as JSON-SEQ.
+
 ## Supported output formats
 
 The following output formats are supported:
index c73f42d0de0de76fc337e2e61c659ce27b4c9356..461c14de631f958870da7e88c84d839167d24e0e 100644 (file)
@@ -43,6 +43,7 @@ def test_args(path: Optional[Path]) -> None:
             "Force": 9001,
             "GenkeyCommonName": "test",
             "GenkeyValidDays": "100",
+            "Json": false,
             "Pager": true,
             "Verb": "build"
         }}
@@ -59,6 +60,7 @@ def test_args(path: Optional[Path]) -> None:
         force = 9001,
         genkey_common_name = "test",
         genkey_valid_days = "100",
+        json = False,
         pager = True,
         verb = Verb.build,
     )