-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add section about Tree->HCL2 reconstruction to the README.md
- Loading branch information
1 parent
1dff4d7
commit bf5d3dc
Showing
2 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
Given `example.tf` file with following content | ||
|
||
```terraform | ||
resource "aws_s3_bucket" "bucket" { | ||
bucket = "bucket_id" | ||
force_destroy = true | ||
} | ||
``` | ||
|
||
below code will add a `tags` object to the S3 bucket definition: | ||
|
||
```python | ||
from copy import deepcopy | ||
from lark import Token, Tree | ||
import hcl2 | ||
|
||
|
||
def build_tags_tree(base_indent: int = 0) -> Tree: | ||
# build Tree representing following HCL2 structure | ||
# tags = { | ||
# Name = "My bucket" | ||
# Environment = "Dev" | ||
# } | ||
return Tree('attribute', [ | ||
Tree('identifier', [ | ||
Token('NAME', 'tags') | ||
]), | ||
Token('EQ', '='), | ||
Tree('expr_term', [ | ||
Tree('object', [ | ||
Tree('new_line_or_comment', [ | ||
Token('NL_OR_COMMENT', '\n' + ' ' * (base_indent + 1)), | ||
]), | ||
Tree('object_elem', [ | ||
Tree('identifier', [ | ||
Token('NAME', 'Name') | ||
]), | ||
Token('EQ', '='), | ||
Tree('expr_term', [ | ||
Token('STRING_LIT', '"My bucket"') | ||
]) | ||
]), | ||
Tree('new_line_and_or_comma', [ | ||
Tree('new_line_or_comment', [ | ||
Token('NL_OR_COMMENT', '\n' + ' ' * (base_indent + 1)), | ||
]), | ||
]), | ||
Tree('object_elem', [ | ||
Tree('identifier', [ | ||
Token('NAME', 'Environment') | ||
]), | ||
Token('EQ', '='), | ||
Tree('expr_term', [ | ||
Token('STRING_LIT', '"Dev"') | ||
]) | ||
]), | ||
Tree('new_line_and_or_comma', [ | ||
Tree('new_line_or_comment', [ | ||
Token('NL_OR_COMMENT', '\n' + ' ' * base_indent), | ||
]), | ||
]), | ||
]), | ||
]) | ||
]) | ||
|
||
|
||
def is_bucket_block(tree: Tree) -> bool: | ||
# check whether given Tree represents `resource "aws_s3_bucket" "bucket"` | ||
return tree.data == 'block' and tree.children[2].value == '"bucket"' | ||
|
||
|
||
def insert_tags(tree: Tree, indent: int = 0) -> Tree: | ||
new_children = [*tree.children.copy(), build_tags_tree(indent)] | ||
# add indentation before tags tree | ||
new_children[len(tree.children) - 1] = Tree('new_line_or_comment', [ | ||
Token('NL_OR_COMMENT', '\n ') | ||
]) | ||
# move closing bracket to the new line | ||
new_children.append( | ||
Tree('new_line_or_comment', [ | ||
Token('NL_OR_COMMENT', '\n') | ||
]) | ||
) | ||
return Tree(tree.data, new_children) | ||
|
||
|
||
def process_token(node: Token, indent=0): | ||
print(f'[{indent}]', ' ' * indent, 'token', node.type, node.value) | ||
return deepcopy(node) | ||
|
||
|
||
def process_tree(node: Tree, indent=0) -> Tree: | ||
new_children = [] | ||
print(f'[{indent}]', ' ' * indent, 'tree', node.data) | ||
for child in node.children: | ||
if isinstance(child, Tree): | ||
if is_bucket_block(child): | ||
block_children = child.children.copy() | ||
block_children[3] = insert_tags(block_children[3], indent) # body | ||
child = Tree(child.data, block_children) | ||
|
||
new_children.append(process_tree(child, indent + 1)) | ||
|
||
else: | ||
new_children.append(process_token(child, indent + 1)) | ||
|
||
return Tree(node.data, new_children) | ||
|
||
|
||
def main(): | ||
tree = hcl2.parse(open('example.tf')) | ||
new_tree = process_tree(tree) | ||
reconstructed = hcl2.writes(new_tree) | ||
open('example_reconstructed.tf', 'w').write(reconstructed) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
|
||
``` |