"""CLI job result helpers: logging, JSON stdout, exit code contract.""" from __future__ import annotations import json import logging import sys from collections.abc import Callable from typing import Any def configure_cli_logging() -> None: logging.basicConfig( level=logging.INFO, format="%(asctime)s [%(levelname)s] %(name)s: %(message)s", ) def emit_and_exit(result: Any) -> None: """Print one JSON line to stdout; exit 1 when result.success is False.""" print(json.dumps(result, ensure_ascii=False, default=str)) if isinstance(result, dict) and result.get("success") is False: sys.exit(1) sys.exit(0) def run_cli(action: Callable[[], Any], *, label: str) -> None: """Configure logging, run action, emit JSON result, and exit.""" configure_cli_logging() try: emit_and_exit(action()) except Exception as exc: logging.exception("%s failed", label) emit_and_exit({"success": False, "error": str(exc)})