"""加载本地开发环境变量(cursor.env 优先级最高)。""" from __future__ import annotations import os from pathlib import Path ROOT = Path(__file__).resolve().parent.parent ENV_FILES = ( ROOT / ".env", ROOT / ".env.local", ROOT / "backend" / ".env", ROOT / "backend" / ".env.local", ROOT / "cursor.env", ) def read_env_file(path: Path) -> dict[str, str]: values: dict[str, str] = {} if not path.exists(): return values for raw_line in path.read_text(encoding="utf-8").splitlines(): line = raw_line.strip() if not line or line.startswith("#") or "=" not in line: continue key, value = line.split("=", 1) key = key.strip() value = value.strip().strip('"').strip("'") if key: values[key] = value return values def load_dev_env() -> dict[str, str]: merged: dict[str, str] = {} for path in ENV_FILES: merged.update(read_env_file(path)) for key, value in merged.items(): os.environ[key] = value return merged def ssh_config() -> tuple[str, str, str]: """返回 (host, user, password),优先读 cursor.env。""" merged = load_dev_env() host = merged.get("NOMAD_SSH_HOST") or merged.get("DEV_SSH_HOST", "") user = merged.get("NOMAD_SSH_USER") or merged.get("DEV_SSH_USER", "ubuntu") password = merged.get("NOMAD_SSH_PASSWORD") or merged.get("DEV_SSH_PASSWORD", "") if not host or not password: raise RuntimeError( "SSH 配置缺失:请在项目根目录 cursor.env 中设置 DEV_SSH_HOST / DEV_SSH_PASSWORD" ) return host, user, password