File size: 1,718 Bytes
1177cc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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()