-
Notifications
You must be signed in to change notification settings - Fork 1
/
validate_json.py
42 lines (31 loc) · 1.32 KB
/
validate_json.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
from typing import Dict
import json
import sys
def validate(filename):
with open(filename) as file:
try:
parsed = json.load(file) # put JSON-data to a variable
return parsed['service_name'], parsed['description'], \
parsed['package_path'], parsed['service_path'], \
parsed['working_dir'], parsed['env_variables']
except Exception:
print("Invalid JSON") # in case json is invalid
sys.exit(1)
else:
print("Valid JSON") # in case json is valid
def generate_override_conf(env_variables: Dict):
environments = ''
for k, v in env_variables.items():
environments += f'Environment="{k}={v}"\n'
return f"[Service]\n{environments}"
if not len(sys.argv) > 1:
sys.exit(1)
filename = sys.argv[1]
service_name, description, package_path, service_path, working_dir, env_variables = validate(filename)
# parse env_variables to the content of ${service_name}.d/override.conf file
override_conf = generate_override_conf(env_variables)
# generate the output for the create-service.sh program
print("{};{};{};{};{};{}".format(service_name, description,
package_path, service_path,
working_dir, override_conf))
sys.exit(0)