-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathconfigs_in_env.py
70 lines (54 loc) · 1.75 KB
/
configs_in_env.py
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
59
60
61
62
63
64
65
66
67
68
69
70
import gzip
import json
import os
import subprocess
from argparse import ArgumentParser
from base64 import b64decode
from base64 import b64encode
from pathlib import Path
CONFIG_FILES = (
"api/client_secrets.json",
"api/config.yaml",
"frontend/lib/config.json",
)
BEANS_CONFIG_ENV = "YELP_BEANS_CONFIG"
def create_parser() -> ArgumentParser:
parser = ArgumentParser()
parser.add_argument("action", choices=("print", "load_from_env"))
return parser
def get_git_root() -> Path:
res = subprocess.run(
("git", "rev-parse", "--show-toplevel"),
check=True,
capture_output=True,
text=True,
)
return Path(res.stdout.strip())
def print_config_as_str() -> None:
git_root = get_git_root()
configs = {}
for config_filepath in CONFIG_FILES:
config_file = git_root / config_filepath
configs[config_filepath] = config_file.read_text()
configs_as_str = json.dumps(configs)
print(b64encode(gzip.compress(configs_as_str.encode())).decode())
def load_config_from_env() -> None:
if BEANS_CONFIG_ENV not in os.environ:
print(f"{BEANS_CONFIG_ENV} not in the environment. Not writing configs")
return
git_root = get_git_root()
configs_as_str = gzip.decompress(b64decode(os.environ[BEANS_CONFIG_ENV]))
configs = json.loads(configs_as_str)
for config_filepath, config in configs.items():
config_file = git_root / config_filepath
config_file.write_text(config)
def main():
args = create_parser().parse_args()
if args.action == "print":
print_config_as_str()
elif args.action == "load_from_env":
load_config_from_env()
else:
raise ValueError("Unsupported action")
if __name__ == "__main__":
main()