#!/usr/bin/env python3 """Load the bundled starter recipe catalog as one atomic operation.""" from __future__ import annotations import argparse import json import os import sys from typing import Sequence _ = sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) from app.database import SessionLocal from app.services.starter_recipe_catalog import ( StarterRecipeCatalogError, load_bundled_starter_catalog, ) def main(argv: Sequence[str] | None = None) -> int: parser = argparse.ArgumentParser(description="Load the bundled starter recipe catalog") parser.add_argument( "--dry-run", action="store_true", help="Validate the catalog and report planned inserts or updates without writing", ) parser.add_argument( "--update-existing", action="store_true", help="Atomically synchronize all existing bundled recipes in place", ) args = parser.parse_args(argv) db = SessionLocal() try: summary = load_bundled_starter_catalog( db, dry_run=args.dry_run, update_existing=args.update_existing, ) print(json.dumps(summary, sort_keys=True)) return 0 except StarterRecipeCatalogError as exc: db.rollback() print(json.dumps({"dry_run": args.dry_run, "error": str(exc)}, sort_keys=True)) return 1 except Exception: db.rollback() raise finally: db.close() if __name__ == "__main__": raise SystemExit(main())