-
Notifications
You must be signed in to change notification settings - Fork 9
/
terraform-docs-parser.py
executable file
·77 lines (58 loc) · 1.99 KB
/
terraform-docs-parser.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
#!/usr/bin/python3
import re
import sys
FIELD_REG = re.compile('\* (.+)$')
FIELD_NAME_REG = re.compile('`(.+)` - (.+)')
LEVEL_REG = re.compile('The `(.+)` block supports:')
class Field:
def __init__(self, name, parent, description):
self.name = name
self.parent = parent
self.description = description
def __repr__(self):
if self.parent:
return f"parent: '{self.parent}'; name: '{self.name}'; description '{self.description}'"
return f"name: '{self.name}'; description '{self.description}'"
def read_file(filename) -> str:
with open(filename) as f:
return f.read()
def write_file(filename: str, content: str):
with open(filename, 'w') as f:
return f.write(content)
def parse_fields(content) -> [str]:
fields = []
level = ''
for line in content.split('\n'):
m = FIELD_REG.match(line)
if m:
field = m.groups()[0]
m = FIELD_NAME_REG.match(field)
if m:
fields.append(Field(m.group(1), level, m.group(2)))
else:
m = LEVEL_REG.match(line)
if m:
level = m.group(1)
return fields
def insert_comment(api_content: str, descriptions: [Field]) -> str:
result = []
for line in api_content.split('\n'):
if d := match_description(line, descriptions):
result.append(f' // {d.description}')
result.append(line)
return '\n'.join(result)
def match_description(line: str, descriptions: [Field]) -> Field or None:
for d in descriptions:
if f'tf:"{d.name}"' in line:
return d
if f'tf:"{d.name},' in line:
return d
return None
if __name__ == '__main__':
doc_filename = sys.argv[1]
api_filename = sys.argv[2]
doc_content = read_file(doc_filename)
fields = parse_fields(doc_content)
api_content = read_file(api_filename)
api_content = insert_comment(api_content, fields)
write_file(api_filename, api_content)