#!/usr/bin/env python3 """Print Gitea config for current user as shell-exportable variables. Usage (bash): eval "$(python scripts/_get_gitea_config.py)" Usage (batch): for /f "usebackq tokens=1,* delims= " %%a in ( `python scripts/_get_gitea_config.py --batch 2^>nul` ) do set "%%b" Config: ~/.gitea/config.yaml — multi-profile YAML. Env: GITEA_USER selects the profile (required). Fallback: scripts/.env (backwards compat, no GITEA_USER needed). """ import os import sys SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) CONFIG_PATH = os.path.expanduser("~/.gitea/config.yaml") ENV_PATH = os.path.join(SCRIPT_DIR, ".env") def _read_yaml_config(path): import yaml with open(path) as f: return yaml.safe_load(f) or {} def main(): use_batch = "--batch" in sys.argv prefix = "set" if use_batch else "export" # 1) Primary: ~/.gitea/config.yaml if os.path.exists(CONFIG_PATH): user = os.environ.get("GITEA_USER") if not user: print( "Error: GITEA_USER is not set. " "Choose from: " + ", ".join(_read_yaml_config(CONFIG_PATH).keys()), file=sys.stderr, ) sys.exit(1) config = _read_yaml_config(CONFIG_PATH) profile = config.get(user) if not profile: print(f"Error: user '{user}' not found in {CONFIG_PATH}", file=sys.stderr) sys.exit(1) print(f'{prefix} GITEA_URL={profile.get("url", "")}') print(f'{prefix} GITEA_REPO={profile.get("repo", "")}') print(f'{prefix} GITEA_API_TOKEN={profile.get("token", "")}') print(f'{prefix} GITEA_USER={user}') return # 2) Fallback: scripts/.env if os.path.exists(ENV_PATH): print(f"Warning: {CONFIG_PATH} not found, falling back to {ENV_PATH}", file=sys.stderr) with open(ENV_PATH) as f: for line in f: line = line.strip() if line.startswith("export "): var = line[7:] if use_batch: var = var.replace("export ", "set ", 1) print(var) if use_batch: print(f"set GITEA_USER={os.environ.get('GITEA_USER', '')}") else: print(f"export GITEA_USER={os.environ.get('GITEA_USER', '')}") return print(f"Error: {CONFIG_PATH} not found. Create it or set up scripts/.env.", file=sys.stderr) sys.exit(1) if __name__ == "__main__": main()