from __future__ import annotations import argparse import json import sys from .feishu import FeishuClient from .postgres import query_postgres from .service import run_feishu_summary, run_sync_brand from .settings import build_app_config def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser(description="Feishu Base and PostgreSQL helper") parser.add_argument( "--action", choices=["feishu", "postgres", "sync-brand", "all"], default="feishu", help="要执行的动作,默认 feishu", ) return parser.parse_args() def main() -> int: try: args = parse_args() config = build_app_config() if config.env_path: print(f"已加载环境变量文件: {config.env_path}", file=sys.stderr) else: print("WARNING: 未找到 .env 文件,将仅使用当前 shell 环境变量", file=sys.stderr) client = FeishuClient(config) result: dict[str, object] = {} if args.action in {"feishu", "all"}: result["feishu"] = run_feishu_summary(client) if args.action in {"postgres", "all"}: result["postgres"] = query_postgres(config) if args.action in {"sync-brand", "all"}: postgres_result = result.get("postgres") result["sync_brand"] = run_sync_brand(config, client, postgres_result if isinstance(postgres_result, dict) else None) print(json.dumps(result, ensure_ascii=False, indent=2)) return 0 except Exception as exc: # noqa: BLE001 print(f"执行失败: {exc}", file=sys.stderr) return 1