-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhcl_ast.go
270 lines (194 loc) · 6.73 KB
/
hcl_ast.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package main
import (
"bytes"
"flag"
"fmt"
"github.com/antonbabenko/tfvars-annotations/util"
"github.com/hashicorp/hcl"
"github.com/hashicorp/hcl/hcl/ast"
"github.com/hashicorp/hcl/hcl/printer"
"github.com/sirupsen/logrus"
//"github.com/hashicorp/hcl/hcl/token"
"io/ioutil"
"reflect"
"regexp"
"sort"
"strings"
"github.com/rodaine/hclencoder"
"github.com/davecgh/go-spew/spew"
)
var (
// String marker
tfvarsDisableAnnotations = `@tfvars:disable_annotations`
// Regexp
tfvarsTerragruntOutputRegexp = regexp.MustCompile(`@tfvars:terragrunt_output\.[^ \n]+`)
_ = spew.Config
_ = fmt.Sprint()
)
func parseContent(hclString *string) (*ast.File, error) {
astf, err := hcl.Parse(*hclString)
if err != nil {
return nil, err
}
return astf, nil
}
func scanComments(astf *ast.File) (bool, []string) {
comments := astf.Comments
isDisabled := false
keysFound := []string{}
for _, commentList := range comments {
for _, comment := range commentList.List {
if strings.Contains(comment.Text, tfvarsDisableAnnotations) {
isDisabled = true
}
allKeys := tfvarsTerragruntOutputRegexp.FindAllString(comment.Text, -1)
keysFound = append(keysFound, allKeys...)
}
}
sort.Strings(keysFound)
keysFound = util.UniqueNonEmptyElementsOf(keysFound)
log.Debugf("Found keys: %s", keysFound)
return isDisabled, keysFound
}
func updateValuesInTfvarsFile(astf *ast.File, allKeyValues map[string]interface{}) (ast.File, []string) {
var errors []string
var hclContent string
flag.Parse()
if *debug == true {
log.Level = logrus.TraceLevel
} else {
log.Level = logrus.InfoLevel
}
ast.Walk(astf.Node, func(n ast.Node) (ast.Node, bool) {
if n == nil {
return n, false
}
typeName := reflect.TypeOf(n).String()
if typeName == "*ast.ObjectItem" {
//log.Traceln("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *")
//log.Traceln("Node type=", typeName)
//log.Traceln("Node=")
//log.Traceln(spew.Sdump(n))
leadCommentText := ""
lineCommentText := ""
//spew.Dump(n.(*ast.ObjectItem).Keys[0])
leadComment := n.(*ast.ObjectItem).LeadComment
if leadComment != nil {
leadCommentText = leadComment.List[0].Text
}
lineComment := n.(*ast.ObjectItem).LineComment
if lineComment != nil {
lineCommentText = lineComment.List[0].Text
log.Traceln("Found line comment:", lineCommentText)
for key, value := range allKeyValues {
if strings.Contains(lineCommentText, key) {
//lineComment.List[0].Text = lineCommentText // + "!!!"
currentVal := n.(*ast.ObjectItem).Val
currentValPos := currentVal.Pos().Line
//log.Traceln("Current line number of the value: ", currentValPos)
valueType := reflect.TypeOf(value).String() // real value type
//desiredValueType := reflect.TypeOf(value).String() // desired value type (to_list will set this to `list`)
log.Tracef("Found line comment value to replace using value from key %s", key)
log.Traceln(spew.Sdump(value))
log.Traceln(spew.Sdump(valueType))
hclBytes, err := hclencoder.Encode(value)
if err != nil {
log.Warnln("Error during hclencoder: ", err)
}
// Create HCL string with properly encoded values and with real number of newlines prefixed to be able to replace in current value
prefixNewlines := strings.Repeat("\n", currentValPos-1)
//hclString := strings.TrimSuffix(string(hclBytes), "\n")
hclString := string(hclBytes)
// @todo: Maps are not supported yet, because comments are placed in strange places
if valueType == "map[string]interface {}" {
//hclContent = prefixNewlines + `key = {` + hclString + `}`
continue
} else {
split := strings.Split(key, ".")
convertToType := ""
if len(split) > 3 {
convertToType = split[3]
}
if convertToType == "to_list" {
hclContent = prefixNewlines + `key = [` + hclString + `]`
} else {
hclContent = prefixNewlines + `key = ` + hclString
}
}
//log.Traceln("HCL content created from the new value to parse to AST: ", hclContent)
astfNew, err2 := hcl.Parse(hclContent)
if err2 != nil {
log.Warnln(err2)
continue
}
// Value from the first element item created earlier (new value)
newVal := astfNew.Node.(*ast.ObjectList).Items[0].Val
//log.Traceln("+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +")
//log.Traceln("astfNew.Node=", spew.Sdump(astfNew.Node))
//log.Traceln("NEW VALUE:", spew.Sdump(newVal))
//log.Traceln("NEW VALUE OFFSET:", spew.Sdump(newVal.Pos().Offset))
//log.Traceln("OLD VALUE:", spew.Sdump(currentVal))
//log.Traceln("OLD VALUE OFFSET:", spew.Sdump(currentVal.Pos().Offset))
// Replacing old value
n.(*ast.ObjectItem).Val = newVal
}
}
}
//spew.Dump(leadComment)
//spew.Dump(lineComment)
_ = leadCommentText
_ = lineCommentText
}
return n, true
})
if false {
log.Traceln("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =")
spew.Dump(astf.Comments)
for i, commentGroup := range astf.Comments {
for _, comment := range commentGroup.List {
log.Traceln("ORIGINAL i=", i, "; offset=", commentGroup.Pos().Offset, "; commentOffset=", comment.Pos().Offset, spew.Sdump(comment))
//lineNumber := comment.Pos().Line
//var lineNumber, columnNumber, Offset int
lineNumber := comment.Pos().Line
columnNumber := commentGroup.Pos().Column
Offset := commentGroup.Pos().Offset
if i == 2 {
lineNumber += 5
//Offset += 10 // number of chars to shift?
}
if i == 4 {
lineNumber += 5
//Offset += 10 // number of chars to shift?
}
// @todo: Adjust Line to it bigger on the length of the previous block
/*comment.Start = token.Pos{
Filename: "",
Offset: Offset,
Line: lineNumber,
Column: columnNumber,
}*/
fmt.Println(lineNumber, columnNumber, Offset)
log.Traceln("NEW i=", i, "; offset=", comment.Pos().Offset, spew.Sdump(comment))
}
}
log.Traceln("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =")
spew.Dump(astf.Comments)
}
//log.Traceln("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =")
//log.Traceln("Complete astf after update:", spew.Sdump(astf))
return *astf, errors
}
func fprintToFile(astf *ast.File, filename string) ([]byte, error) {
var buf bytes.Buffer
if err := printer.Fprint(&buf, astf); err != nil {
return buf.Bytes(), err
}
// Add trailing newline to result to prevent from reformatting every time
buf.WriteString("\n")
if filename != "" {
if err := ioutil.WriteFile(filename, buf.Bytes(), 0644); err != nil {
return buf.Bytes(), err
}
}
return buf.Bytes(), nil
}