-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbenv.py
85 lines (58 loc) · 2.19 KB
/
dbenv.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python3
def get_config():
# get environment form json storage
with open("db_environments.json", "r") as config_file:
configs = json.load(config_file)
try:
db_env = configs[env]
# replace existing databricks configuration
with open(".databricks-connect", "w") as db_connect_env:
json.dump(db_env, db_connect_env, indent=4)
except KeyError:
print(f"no databricks environment {env} is available. try to set it")
def set_config(env_name: str):
# add a new configuration to the json storage
# get environment form json storage
with open("db_environments.json", 'r') as config_file:
try:
configs = json.load(config_file)
except (JSONDecodeError, TypeError):
configs = dict()
new_env = dict()
for param in ["host", "token", "cluster_id", "org_id", "port"]:
value = str(input(f"{param}: "))
new_env[param] = value
configs[env_name] = new_env
with open("db_environments.json", "w") as config_file:
configs = json.dump(configs, config_file)
def ls_config():
with open("db_environments.json", 'r') as config_file:
try:
configs = json.load(config_file)
except (JSONDecodeError, TypeError) as e:
configs = dict()
print(e)
print("\nfollowing environments are available:")
for key, values in configs.items():
print(f"\nEnvironment: {key}")
for param, setting in values.items():
print(f"{param}: {setting}")
if __name__ == "__main__":
import sys
import json
from json.decoder import JSONDecodeError
# get arguments from command line
script = sys.argv[0]
action = sys.argv[1]
try:
env = sys.argv[2]
except IndexError:
env = ""
# check if action is available
assert action in ["-get", "-set", "-ls"], f"action is missing please check available options (-ls, -get or -set)"
if action == "-get":
get_config()
elif action == "-set":
set_config(env_name=env)
else:
ls_config()