Skip to content

Commit f588749

Browse files
author
Christophe Guerreiro
committed
spdx-diff: cli: Change output format to JSON and enfore JSON filename argument
Previously, the tool was printing human-readable text to stdout and generating a JSON file separately. This change enforces the presence of an explicit argument used as the JSON output filename. The console output is now also emitted in JSON format to ensure consistency and to simplify integration with automated pipelines and tooling. Signed-off-by: Christophe Guerreiro <christophe.guerreiro@non.se.com>
1 parent 2f29719 commit f588749

1 file changed

Lines changed: 13 additions & 16 deletions

File tree

src/spdx_diff/cli.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from argparse import ArgumentParser, ArgumentTypeError, BooleanOptionalAction
1010
from collections import defaultdict
1111
from contextlib import redirect_stdout
12-
from datetime import datetime, timezone
1312
from typing import Any
1413

1514
from . import __version__
@@ -382,8 +381,15 @@ def write_diff_to_json(
382381
"changed": dict(sorted(pcfg_diff[2].items())),
383382
},
384383
}
385-
with output_file.open("w", encoding="utf-8") as f:
386-
json.dump(delta, f, indent=2, ensure_ascii=False)
384+
# Write the resulting SPDX diff JSON to stdout for piping
385+
json.dump(delta, sys.stdout, indent=2, ensure_ascii=False)
386+
sys.stdout.write("\n")
387+
388+
# Write the resulting SPDX diff JSON to file
389+
if output_file is not None:
390+
_logger.info("Writing diff results to %s", output_file)
391+
with output_file.open("w", encoding="utf-8") as f:
392+
json.dump(delta, f, indent=2, ensure_ascii=False)
387393

388394

389395
def path_is_file(value: str) -> pathlib.Path:
@@ -426,21 +432,13 @@ def main() -> None:
426432
type=path_is_file,
427433
help="New SPDX3 JSON file",
428434
)
429-
timestamp = datetime.now(tz=timezone.utc).astimezone().strftime("%Y%m%d-%H%M%S")
430-
default_output = f"spdx_diff_{timestamp}.json"
431435
parser.add_argument(
432-
"--output",
436+
"--json-output",
433437
"-o",
434438
metavar="PATH",
435439
type=pathlib.Path,
436-
default=default_output,
437-
help="Optional output file name (JSON)",
438-
)
439-
parser.add_argument(
440-
"--format",
441-
choices=["text", "json", "both"],
442-
default="both",
443-
help="Output format: text (console only), json (file only), or both (default)",
440+
default=None,
441+
help="JSON Output file name (default: none)",
444442
)
445443

446444
# Output filtering category options
@@ -507,8 +505,6 @@ def main() -> None:
507505
pcfg_diff[2],
508506
)
509507

510-
if args.format in ["json", "both"]:
511-
write_diff_to_json(pkg_diff, cfg_diff, pcfg_light_diff, args.output)
512508
# Print human readable information on stderr
513509
with redirect_stdout(sys.stderr):
514510
if show_packages:
@@ -527,6 +523,7 @@ def main() -> None:
527523
)
528524

529525

526+
write_diff_to_json(pkg_diff, cfg_diff, pcfg_light_diff, args.json_output)
530527

531528

532529
if __name__ == "__main__":

0 commit comments

Comments
 (0)