diff --git a/3.snapshot_comparator/src/snapshot_comparator/cli.py b/3.snapshot_comparator/src/snapshot_comparator/cli.py new file mode 100644 index 0000000..6d7f373 --- /dev/null +++ b/3.snapshot_comparator/src/snapshot_comparator/cli.py @@ -0,0 +1,52 @@ +import argparse +import json +import os +from pathlib import Path +from typing import Any + +from snapshot_comparator.core import compare_snapshots + + +def _load_json_snapshot(path: Path) -> list[dict[str, Any]]: + if not path.exists(): + raise FileNotFoundError(f"Snapshot file not found: {path}") + with path.open('r', encoding='utf-8') as f: + try: + data = json.load(f) + except json.JSONDecodeError as e: + raise ValueError(f"Invalid JSON in {path}: {e}") + if not isinstance(data, list): + raise TypeError(f"Expected list of snapshots, got {type(data)} in {path}") + return data + + +def main() -> None: + parser = argparse.ArgumentParser(description="Vergleicht zwei Snapshot-JSON-Dateien.") + parser.add_argument("--before", required=True, help="Pfad zur ersten Snapshot-JSON-Datei") + parser.add_argument("--after", required=True, help="Pfad zur zweiten Snapshot-JSON-Datei") + parser.add_argument("--out", required=False, help="Optionaler Ausgabepfad für Vergleichsergebnisse") + + args = parser.parse_args() + + before_path = Path(args.before).resolve() + after_path = Path(args.after).resolve() + out_path = Path(args.out).resolve() if args.out else Path("output/comparison_results.json").resolve() + + # Eingabevalidierung + assert before_path.exists() and before_path.is_file(), f"Invalid input file: {before_path}" + assert after_path.exists() and after_path.is_file(), f"Invalid input file: {after_path}" + + snapshot1 = _load_json_snapshot(before_path) + snapshot2 = _load_json_snapshot(after_path) + + result = compare_snapshots(snapshot1, snapshot2) + + os.makedirs(out_path.parent, exist_ok=True) + with open(out_path, 'w', encoding='utf-8') as f: + json.dump(result, f, indent=2, ensure_ascii=False) + + print(f"Snapshot comparison written to {out_path}") + + +if __name__ == "__main__": + main()