generated from getupcloud/terraform-cluster-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-modules
executable file
·31 lines (26 loc) · 941 Bytes
/
make-modules
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
#!/usr/bin/env python3
import sys
import json
from hcl2.parser import hcl2
display_path = '<stdin>' if len(sys.argv) < 2 else sys.argv[1]
path = sys.stdin if len(sys.argv) < 2 else open(sys.argv[1], 'r')
data = hcl2.parse(path.read())
indent = 2
locals_block = {
'//': ['This file was auto-generated by command:', f'$ {sys.argv[0]} {display_path}']
}
def update(d, parents=[]):
for k, v in d.items():
if isinstance(v, dict):
d[k] = update(d[k], parents + [k])
else:
path = '.'.join(parents + [k])
d[k] = f'${{try(var.modules.{path}, var.modules_defaults.{path})}}'
return d
for variables in data['variable']:
for var_name, var_value in variables.items():
if var_name != 'modules_defaults':
continue
default = var_value['default']
locals_block['locals'] = { 'modules': update(default) }
print(json.dumps(locals_block,indent=indent))