-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_signature_example_test.go
119 lines (100 loc) · 3.02 KB
/
block_signature_example_test.go
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package tfsig_test
import (
"fmt"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/zclconf/go-cty/cty"
"github.com/yoanm/go-tfsig"
)
func Example() {
// Create a resource block
sig := tfsig.NewResource("res_name", "res_id")
sig.AppendAttribute("attribute1", cty.StringVal("value1"))
sig.AppendEmptyLine()
sig.AppendAttribute("attribute2", cty.BoolVal(true))
sig.AppendAttribute("attribute3", cty.NumberFloatVal(-12.34))
sig.AppendEmptyLine()
hclFile := hclwrite.NewEmptyFile()
hclFile.Body().AppendBlock(sig.Build())
fmt.Println(string(hclFile.Bytes()))
// Output:
// resource "res_name" "res_id" {
// attribute1 = "value1"
//
// attribute2 = true
// attribute3 = -12.34
//
// }
}
func Example_enhance_existing() {
// Enhance an existing signature
sig := tfsig.NewResource("res_name", "res_id")
sig.AppendAttribute("attribute1", cty.StringVal("value1"))
sig.AppendEmptyLine()
sig.AppendAttribute("attribute2", cty.BoolVal(true))
sig.AppendAttribute("attribute3", cty.NumberFloatVal(-12.34))
sig.AppendEmptyLine()
// ... Later in the code
// Enhance signature by removing empty lines and add an attribute below attribute2
newElems := tfsig.BodyElements{}
for _, elem := range sig.GetElements() {
// Remove all empty lines
if !elem.IsBodyEmptyLine() {
newElems = append(newElems, elem)
}
// Append a new attribute right after attribute2
if elem.GetName() == "attribute2" {
newElems = append(newElems, tfsig.NewBodyAttribute("attribute22", cty.BoolVal(false)))
}
}
sig.SetElements(newElems)
// ... Finally
hclFile := hclwrite.NewEmptyFile()
hclFile.Body().AppendBlock(sig.Build())
fmt.Println(string(hclFile.Bytes()))
// Output:
// resource "res_name" "res_id" {
// attribute1 = "value1"
// attribute2 = true
// attribute22 = false
// attribute3 = -12.34
// }
}
func Example_reorder() {
// Reorder an existing signature
sig := tfsig.NewResource("res_name", "res_id")
sig.AppendAttribute("attribute1", cty.StringVal("value1"))
sig.AppendAttribute("attribute2", cty.BoolVal(true))
sig.AppendAttribute("attribute3", cty.NumberFloatVal(-12.34))
sig.AppendEmptyLine()
sig.AppendAttribute("attribute4", cty.NumberIntVal(42))
sig.AppendAttribute("attribute5", cty.StringVal("value5"))
// ... Later in the code
// Re-order elements and remove empty lines
newElems := make(tfsig.BodyElements, len(sig.GetElements()))
extraElemCont := 0
for _, elem := range sig.GetElements() {
switch {
case elem.GetName() == "attribute1":
newElems[2] = elem
case elem.GetName() == "attribute4":
newElems[1] = elem
case elem.GetName() == "attribute3":
newElems[0] = elem
case !elem.IsBodyEmptyLine():
newElems[3+extraElemCont] = elem
extraElemCont++
}
}
sig.SetElements(newElems)
hclFile := hclwrite.NewEmptyFile()
hclFile.Body().AppendBlock(sig.Build())
fmt.Println(string(hclFile.Bytes()))
// Output:
// resource "res_name" "res_id" {
// attribute3 = -12.34
// attribute4 = 42
// attribute1 = "value1"
// attribute2 = true
// attribute5 = "value5"
// }
}