-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse.go
291 lines (265 loc) · 8.1 KB
/
parse.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package tfplanparse
import (
"bufio"
"fmt"
"io"
"os"
"strings"
)
const (
NO_CHANGES_STRING = "No changes. Infrastructure is up-to-date."
CHANGES_START_STRING = "Terraform will perform the following actions:"
CHANGES_END_STRING = "Plan: "
ERROR_STRING = "Error: "
)
func Parse(input io.Reader) ([]*ResourceChange, error) {
result := []*ResourceChange{}
parse := false
scanner := bufio.NewScanner(input)
for scanner.Scan() {
text := formatInput(scanner.Bytes())
if text == "" {
continue
}
if !parse {
if strings.Contains(text, NO_CHANGES_STRING) || strings.Contains(text, ERROR_STRING) {
// Nothing to parse, return empty plan
return result, nil
} else if strings.Contains(text, CHANGES_START_STRING) {
// Parse all lines from here on
parse = true
}
continue
}
if IsResourceCommentLine(text) {
rc, err := parseResource(scanner)
if err != nil {
return nil, err
}
result = append(result, rc)
}
if strings.Contains(formatInput(scanner.Bytes()), CHANGES_END_STRING) {
// we are done
return result, nil
}
}
return nil, fmt.Errorf("unexpected end of input while parsing plan")
}
func ParseFromFile(filepath string) ([]*ResourceChange, error) {
f, err := os.Open(filepath)
if err != nil {
return []*ResourceChange{}, err
}
return Parse(f)
}
func parseResource(s *bufio.Scanner) (*ResourceChange, error) {
rc, err := NewResourceChangeFromComment(formatInput(s.Bytes()))
if err != nil {
return nil, err
}
for s.Scan() {
text := formatInput(s.Bytes())
switch {
case IsResourceTerminator(text):
return rc, nil
case IsResourceCommentLine(text), strings.Contains(text, CHANGES_END_STRING):
return nil, fmt.Errorf("unexpected line while parsing resource attribute: %s", text)
case IsMapAttributeChangeLine(text):
ma, err := parseMapAttribute(s)
if err != nil {
return nil, err
}
rc.AttributeChanges = append(rc.AttributeChanges, ma)
case IsArrayAttributeChangeLine(text):
aa, err := parseArrayAttribute(s)
if err != nil {
return nil, err
}
rc.AttributeChanges = append(rc.AttributeChanges, aa)
case IsJSONEncodeAttributeChangeLine(text):
ja, err := parseJSONEncodeAttribute(s)
if err != nil {
return nil, err
}
rc.AttributeChanges = append(rc.AttributeChanges, ja)
case IsHeredocAttributeChangeLine(text):
ha, err := parseHeredocAttribute(s)
if err != nil {
return nil, err
}
rc.AttributeChanges = append(rc.AttributeChanges, ha)
case IsAttributeChangeLine(text):
ac, err := NewAttributeChangeFromLine(text)
if err != nil {
return nil, err
}
rc.AttributeChanges = append(rc.AttributeChanges, ac)
}
}
return nil, fmt.Errorf("unexpected end of input while parsing resource")
}
func parseMapAttribute(s *bufio.Scanner) (*MapAttributeChange, error) {
normalized := formatInput(s.Bytes())
result, err := NewMapAttributeChangeFromLine(normalized)
if err != nil {
return nil, err
}
if IsOneLineEmptyMapAttribute(normalized) {
return result, nil
}
for s.Scan() {
text := formatInput(s.Bytes())
switch {
case IsMapAttributeTerminator(text):
return result, nil
case IsResourceCommentLine(text), strings.Contains(text, CHANGES_END_STRING):
return nil, fmt.Errorf("unexpected line while parsing map attribute: %s", text)
case IsMapAttributeChangeLine(text):
ma, err := parseMapAttribute(s)
if err != nil {
return nil, err
}
result.AttributeChanges = append(result.AttributeChanges, ma)
case IsArrayAttributeChangeLine(text):
aa, err := parseArrayAttribute(s)
if err != nil {
return nil, err
}
result.AttributeChanges = append(result.AttributeChanges, aa)
case IsJSONEncodeAttributeChangeLine(text):
ja, err := parseJSONEncodeAttribute(s)
if err != nil {
return nil, err
}
result.AttributeChanges = append(result.AttributeChanges, ja)
case IsHeredocAttributeChangeLine(text):
ha, err := parseHeredocAttribute(s)
if err != nil {
return nil, err
}
result.AttributeChanges = append(result.AttributeChanges, ha)
case IsAttributeChangeLine(text):
ac, err := NewAttributeChangeFromLine(text)
if err != nil {
return nil, err
}
result.AttributeChanges = append(result.AttributeChanges, ac)
}
}
return nil, fmt.Errorf("unexpected end of input while parsing map attribute")
}
func parseArrayAttribute(s *bufio.Scanner) (*ArrayAttributeChange, error) {
normalized := formatInput(s.Bytes())
result, err := NewArrayAttributeChangeFromLine(normalized)
if err != nil {
return nil, err
}
if IsOneLineEmptyArrayAttribute(normalized) {
return result, nil
}
// TODO: all elements of array attributes are the same type
for s.Scan() {
text := formatInput(s.Bytes())
switch {
case IsArrayAttributeTerminator(text):
return result, nil
case IsResourceCommentLine(text), strings.Contains(text, CHANGES_END_STRING):
return nil, fmt.Errorf("unexpected line while parsing array attribute: %s", text)
case IsMapAttributeChangeLine(text):
ma, err := parseMapAttribute(s)
if err != nil {
return nil, err
}
result.AttributeChanges = append(result.AttributeChanges, ma)
case IsArrayAttributeChangeLine(text):
ma, err := parseArrayAttribute(s)
if err != nil {
return nil, err
}
result.AttributeChanges = append(result.AttributeChanges, ma)
case IsJSONEncodeAttributeChangeLine(text):
ja, err := parseJSONEncodeAttribute(s)
if err != nil {
return nil, err
}
result.AttributeChanges = append(result.AttributeChanges, ja)
case IsHeredocAttributeChangeLine(text):
ha, err := parseHeredocAttribute(s)
if err != nil {
return nil, err
}
result.AttributeChanges = append(result.AttributeChanges, ha)
case IsAttributeChangeArrayItem(text):
ac, err := NewAttributeChangeFromArray(text)
if err != nil {
return nil, err
}
result.AttributeChanges = append(result.AttributeChanges, ac)
}
}
return nil, fmt.Errorf("unexpected end of input while parsing array attribute")
}
func parseJSONEncodeAttribute(s *bufio.Scanner) (*JSONEncodeAttributeChange, error) {
normalized := formatInput(s.Bytes())
result, err := NewJSONEncodeAttributeChangeFromLine(normalized)
if err != nil {
return nil, err
}
// TODO: check if oneline check needed
for s.Scan() {
text := formatInput(s.Bytes())
switch {
case IsJSONEncodeAttributeTerminator(text):
return result, nil
case IsResourceCommentLine(text), strings.Contains(text, CHANGES_END_STRING):
return nil, fmt.Errorf("unexpected line while parsing jsonencode attribute: %s", text)
case IsMapAttributeChangeLine(text):
ma, err := parseMapAttribute(s)
if err != nil {
return nil, err
}
result.AttributeChanges = append(result.AttributeChanges, ma)
case IsArrayAttributeChangeLine(text):
aa, err := parseArrayAttribute(s)
if err != nil {
return nil, err
}
result.AttributeChanges = append(result.AttributeChanges, aa)
case IsHeredocAttributeChangeLine(text):
// TODO: check if this is even allowed by terraform
ha, err := parseHeredocAttribute(s)
if err != nil {
return nil, err
}
result.AttributeChanges = append(result.AttributeChanges, ha)
case IsAttributeChangeLine(text):
// TODO: check if this is even allowed by terraform
ac, err := NewAttributeChangeFromLine(text)
if err != nil {
return nil, err
}
result.AttributeChanges = append(result.AttributeChanges, ac)
}
}
return nil, fmt.Errorf("unexpected end of input while parsing jsonencode attribute")
}
func parseHeredocAttribute(s *bufio.Scanner) (*HeredocAttributeChange, error) {
normalized := formatInput(s.Bytes())
result, err := NewHeredocAttributeChangeFromLine(normalized)
if err != nil {
return nil, err
}
for s.Scan() {
text := formatInput(s.Bytes())
if IsHeredocAttributeTerminator(text) {
return result, nil
}
// TODO: should not trim space for heredoc, but only trim the indent
// TODO: it's also hard to determine if a line was deleted or is a list in an array
result.AddLineToContent(text)
}
return nil, fmt.Errorf("unexpected end of input while parsing heredoc attribute")
}
func formatInput(input []byte) string {
return strings.TrimSpace(uncolor(input))
}