-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen-cfg.py
executable file
·35 lines (28 loc) · 1.05 KB
/
gen-cfg.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
#!/usr/bin/env python3
import os
import string
files = [
'nfacctd.conf',
'sql/create-db.sql',
]
# Copied from stack overflow to avoid external dependency:
# https://stackoverflow.com/questions/40216311/reading-in-environment-variables-from-an-environment-file
def get_env_data_as_dict(path: str) -> dict:
with open(path, 'r') as f:
return dict(tuple(line.replace('\n', '').split('=')) for line
in f.readlines() if not line.startswith('#'))
# Load the env variables
env_data = get_env_data_as_dict('./.env')
substitutions = {}
for curr_env in env_data.keys():
try:
substitutions[curr_env] = env_data[curr_env]
except KeyError:
print(f"Please set env variable: {curr_env}")
# Substitute the env variables in the templates
for curr_file in files:
template_file = f"{curr_file}.tpl"
with open(curr_file,'w') as f_config, open(template_file, 'r') as f_template:
template_str = f_template.read()
template = string.Template(template_str).substitute(substitutions)
f_config.write(template)