"""Write YouTube stream key into repo config/youtube.ini while preserving other keys.""" from __future__ import annotations import configparser import io from pathlib import Path from src.config_governance import save_managed_file def write_youtube_ini_stream_key(repo_root: Path, stream_key_or_url: str) -> str: root = repo_root.resolve() cfg_path = root / "config" / "youtube.ini" value = (stream_key_or_url or "").strip() if not value: return "sync skipped: empty youtube_rtmp_key" cfg_path.parent.mkdir(parents=True, exist_ok=True) parser = configparser.ConfigParser() if cfg_path.is_file(): parser.read(cfg_path, encoding="utf-8") if not parser.has_section("youtube"): parser.add_section("youtube") parser.set("youtube", "key", value) buf = io.StringIO() parser.write(buf) save_managed_file("app-config", "youtube.ini", buf.getvalue()) return f"wrote {cfg_path}"