import os import sys import urllib.request import urllib.error ENV_PATH = os.path.join(os.getcwd(), ".env") API_URL = os.environ.get("ENV_MANAGER_URL", "https://env-manage.vercel.app") def main(): try: with open(ENV_PATH, "r", encoding="utf-8") as f: content = f.read() except FileNotFoundError: print(f"[env-manager] .env file not found at {ENV_PATH}", file=sys.stderr) sys.exit(1) project_id = None token = None for line in content.splitlines(): line = line.strip() if line.startswith("ENV_MANAGER_PROJECTID="): project_id = line.split("=", 1)[1].strip() elif line.startswith("ENV_MANAGER_TOKEN="): token = line.split("=", 1)[1].strip() if not project_id or not token: print("[env-manager] Missing ENV_MANAGER_PROJECTID or ENV_MANAGER_TOKEN in .env", file=sys.stderr) sys.exit(1) print("[env-manager] Syncing environment...") try: req = urllib.request.Request( f"{API_URL}/api/get-env", headers={ "X-Env-Manager-Project-ID": project_id, "X-Env-Manager-Token": token, }, ) with urllib.request.urlopen(req) as res: data = res.read().decode("utf-8") except urllib.error.HTTPError as e: print(f"[env-manager] Server responded with {e.code}", file=sys.stderr) sys.exit(1) except Exception as e: print(f"[env-manager] Request failed: {e}", file=sys.stderr) sys.exit(1) with open(ENV_PATH, "w", encoding="utf-8") as f: f.write(data) print("[env-manager] .env synced successfully.") if __name__ == "__main__": main()