Skip to content

Commit c51962f

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 ec50712 commit c51962f

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__
@@ -386,8 +385,15 @@ def write_diff_to_json(
386385
"changed": dict(sorted(pcfg_diff[2].items())),
387386
},
388387
}
389-
with output_file.open("w", encoding="utf-8") as f:
390-
json.dump(delta, f, indent=2, ensure_ascii=False)
388+
# Write the resulting SPDX diff JSON to stdout for piping
389+
json.dump(delta, sys.stdout, indent=2, ensure_ascii=False)
390+
sys.stdout.write("\n")
391+
392+
# Write the resulting SPDX diff JSON to file
393+
if output_file is not None:
394+
_logger.info("Writing diff results to %s", output_file)
395+
with output_file.open("w", encoding="utf-8") as f:
396+
json.dump(delta, f, indent=2, ensure_ascii=False)
391397

392398

393399
def path_is_file(value: str) -> pathlib.Path:
@@ -430,21 +436,13 @@ def main() -> None:
430436
type=path_is_file,
431437
help="New SPDX3 JSON file",
432438
)
433-
timestamp = datetime.now(tz=timezone.utc).astimezone().strftime("%Y%m%d-%H%M%S")
434-
default_output = f"spdx_diff_{timestamp}.json"
435439
parser.add_argument(
436-
"--output",
440+
"--json-output",
437441
"-o",
438442
metavar="PATH",
439443
type=pathlib.Path,
440-
default=default_output,
441-
help="Optional output file name (JSON)",
442-
)
443-
parser.add_argument(
444-
"--format",
445-
choices=["text", "json", "both"],
446-
default="both",
447-
help="Output format: text (console only), json (file only), or both (default)",
444+
default=None,
445+
help="JSON Output file name (default: none)",
448446
)
449447

450448
# Output filtering category options
@@ -511,8 +509,6 @@ def main() -> None:
511509
pcfg_diff[2],
512510
)
513511

514-
if args.format in ["json", "both"]:
515-
write_diff_to_json(pkg_diff, cfg_diff, pcfg_light_diff, args.output)
516512
# Print human readable information on stderr
517513
with redirect_stdout(sys.stderr):
518514
if show_packages:
@@ -531,6 +527,7 @@ def main() -> None:
531527
)
532528

533529

530+
write_diff_to_json(pkg_diff, cfg_diff, pcfg_light_diff, args.json_output)
534531

535532

536533
if __name__ == "__main__":

0 commit comments

Comments
 (0)