From: Daan De Meyer Date: Fri, 20 Oct 2023 13:52:00 +0000 (+0200) Subject: Add --json option X-Git-Tag: v19~57^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F2001%2Fhead;p=thirdparty%2Fmkosi.git Add --json option Let's allow using --json with summary to get a JSON dump of the configuration. --- diff --git a/mkosi/__init__.py b/mkosi/__init__.py index be1a05ba3..7989e46c4 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -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 diff --git a/mkosi/config.py b/mkosi/config.py index ab2207e0d..fb92d5bdb 100644 --- a/mkosi/config.py +++ b/mkosi/config.py @@ -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( diff --git a/mkosi/resources/mkosi.md b/mkosi/resources/mkosi.md index 6dac4b249..9f0020e3c 100644 --- a/mkosi/resources/mkosi.md +++ b/mkosi/resources/mkosi.md @@ -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: diff --git a/tests/test_json.py b/tests/test_json.py index c73f42d0d..461c14de6 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -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, )