-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
754 lines (636 loc) · 17.3 KB
/
main.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
// Copyright 2020 Kyle McCullough. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"context"
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/kylemcc/terragrunt-v19-upgrade/version"
"github.com/genuinetools/pkg/cli"
hclv1ast "github.com/hashicorp/hcl/hcl/ast"
hclv1parser "github.com/hashicorp/hcl/hcl/parser"
hclv1token "github.com/hashicorp/hcl/hcl/token"
hclv2 "github.com/hashicorp/hcl/v2"
hclv2parse "github.com/hashicorp/hcl/v2/hclparse"
hclv2syntax "github.com/hashicorp/hcl/v2/hclsyntax"
hclv2write "github.com/hashicorp/hcl/v2/hclwrite"
"github.com/hashicorp/terraform/tfdiags"
)
const name = "terragrunt-v19-upgrade"
var (
tokNewline = &hclv2write.Token{
Type: hclv2syntax.TokenNewline,
Bytes: []byte{'\n'},
}
tokComma = &hclv2write.Token{
Type: hclv2syntax.TokenComma,
Bytes: []byte{','},
}
tokOBrace = &hclv2write.Token{
Type: hclv2syntax.TokenOBrace,
Bytes: []byte{'{'},
}
tokCBrace = &hclv2write.Token{
Type: hclv2syntax.TokenCBrace,
Bytes: []byte{'}'},
}
tokOBracket = &hclv2write.Token{
Type: hclv2syntax.TokenOBrack,
Bytes: []byte{'['},
}
tokCBracket = &hclv2write.Token{
Type: hclv2syntax.TokenCBrack,
Bytes: []byte{']'},
}
tokOQuote = &hclv2write.Token{
Type: hclv2syntax.TokenOQuote,
Bytes: []byte{'"'},
}
tokCQuote = &hclv2write.Token{
Type: hclv2syntax.TokenCQuote,
Bytes: []byte{'"'},
}
tokEqual = &hclv2write.Token{
Type: hclv2syntax.TokenEqual,
Bytes: []byte{'='},
}
)
var errNotTerragruntConfig = errors.New("file does not contain a terragrunt attribute")
type command struct {
recursive bool
gitMv bool
dryRun bool
keepOld bool
}
func main() {
p := cli.NewProgram()
p.Name = name
p.Version = version.Version
p.GitCommit = version.GitCommit
p.Description = `A tool for upgrading terragrunt configs from terragrunt <= v0.18 to >= v0.19`
var cmd command
p.FlagSet = flag.NewFlagSet("global", flag.ExitOnError)
p.FlagSet.BoolVar(&cmd.recursive, "r", false, "Search subdirectores for terraform.tfvars files")
p.FlagSet.BoolVar(&cmd.recursive, "recursive", false, "Search subdirectores for terraform.tfvars files")
p.FlagSet.BoolVar(&cmd.gitMv, "m", false, "Update files in place and \"git mv terraform.tfvars terragrunt.hcl\"")
p.FlagSet.BoolVar(&cmd.gitMv, "git-mv", false, "Update files in place and \"git mv terraform.tfvars terragrunt.hcl\"")
p.FlagSet.BoolVar(&cmd.dryRun, "d", false, "Do not update any files, just print changes to stdout")
p.FlagSet.BoolVar(&cmd.dryRun, "dry-run", false, "Do not update any files, just print changes to stdout")
p.FlagSet.BoolVar(&cmd.keepOld, "k", false, "Keep old terraform.tfvars files")
p.FlagSet.BoolVar(&cmd.keepOld, "keep", false, "Keep old terraform.tfvars files")
p.Action = cmd.run
p.Run()
}
func (c *command) run(ctx context.Context, args []string) error {
if err := c.validateArgs(args); err != nil {
return err
}
paths, err := c.loadFiles(args)
if err != nil {
return err
}
for _, p := range paths {
orig, err := c.readFile(p)
if err != nil {
return err
}
upgraded, err := c.upgrade(orig)
if err == errNotTerragruntConfig {
fmt.Fprintf(os.Stderr, "warning: ignoring file %s. file does not contain a terragrunt attribute.", p)
continue
} else if err != nil {
return fmt.Errorf("error upgrading file %s: %v", p, err)
}
if err := c.save(p, upgraded); err != nil {
return err
}
}
return nil
}
func (c *command) validateArgs(args []string) error {
if len(args) < 1 {
fmt.Fprintf(os.Stderr, "usage: %s [flags] [file|dir ...|-]\n\n", name)
return flag.ErrHelp
}
if len(args) == 1 && args[0] == "-" {
return nil
}
for _, p := range args {
fi, err := os.Stat(p)
if err != nil {
return err
}
if fi.IsDir() && !c.recursive {
fmt.Fprintf(os.Stderr, "error: %s is a directory\n\n", p)
return flag.ErrHelp
}
}
return nil
}
func (c *command) loadFiles(args []string) ([]string, error) {
var files []string
for _, p := range args {
if p == "-" {
return []string{"-"}, nil
}
fi, err := os.Stat(p)
if err != nil {
return files, err
}
if fi.IsDir() {
if !c.recursive {
fmt.Fprintf(os.Stderr, "warning: recursive option not specified. ignoring directory %s\n", p)
continue
}
err := filepath.Walk(p, func(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if fi.IsDir() && fi.Name() == ".terragrunt-cache" {
return filepath.SkipDir
}
if fi.Name() == "terraform.tfvars" {
files = append(files, path)
}
return nil
})
if err != nil {
return files, err
}
} else {
if fi.Name() != "terraform.tfvars" {
fmt.Fprintf(os.Stderr, "warning: ignoring file %s", p)
continue
}
files = append(files, p)
}
}
return files, nil
}
func (c *command) readFile(path string) ([]byte, error) {
if path == "-" {
return ioutil.ReadAll(os.Stdin)
}
return ioutil.ReadFile(path)
}
// upgrade reads in a terragrunt <= 0.18 config (hcl v1 syntax) and returns
// and upgraded terragrunt >= 0.19 configuration in hcl v2 syntax.
func (c *command) upgrade(input []byte) ([]byte, error) {
res, err := hclv1parser.Parse(input)
if err != nil {
return nil, fmt.Errorf("error parsing file: %v", err)
}
var (
tgSettings []*hclv1ast.ObjectItem
inputVars []*hclv1ast.ObjectItem
)
detachedComments := c.loadDetachedComments(res)
root := res.Node.(*hclv1ast.ObjectList)
for _, item := range root.Items {
item := item
if item.Keys[0].Token.Text == "terragrunt" {
obj := item.Val.(*hclv1ast.ObjectType)
for _, o := range obj.List.Items {
tgSettings = append(tgSettings, o)
}
} else {
inputVars = append(inputVars, item)
}
}
if len(tgSettings) == 0 {
return nil, errNotTerragruntConfig
}
f := hclv2write.NewEmptyFile()
body := f.Body()
c.writeNode(-1, "", body, &hclv1ast.ObjectList{Items: tgSettings}, detachedComments)
if len(inputVars) > 0 {
body.AppendNewline()
inputs := &hclv1ast.ObjectItem{
Keys: []*hclv1ast.ObjectKey{
{
Token: hclv1token.Token{
Type: hclv1token.IDENT,
Pos: inputVars[0].Pos(),
Text: "inputs",
},
},
},
Val: &hclv1ast.ObjectType{
List: &hclv1ast.ObjectList{
Items: inputVars,
},
},
}
c.writeNode(-1, "", body, inputs, detachedComments)
}
if detachedComments.Len() > 0 {
// write out any remaining comments
for _, cg := range *detachedComments {
body.AppendNewline()
c.writeNode(0, "", body, cg, nil)
}
}
return hclv2write.Format(f.Bytes()), nil
}
func (c *command) writeNode(depth int, parentKey string, body *hclv2write.Body, node hclv1ast.Node, cl *commentList) {
// write out any detached comments that should come before the current node
if cl != nil {
comments := cl.PopBefore(node.Pos())
c.writeComments(body, comments)
}
switch nv := node.(type) {
case *hclv1ast.ListType:
oneline := nv.Lbrack.Line == nv.Rbrack.Line
body.AppendUnstructuredTokens(hclv2write.Tokens{tokOBracket})
if !oneline {
body.AppendNewline()
}
for i, n := range nv.List {
if i > 0 {
body.AppendUnstructuredTokens(hclv2write.Tokens{tokComma})
if !oneline {
body.AppendNewline()
}
}
c.writeNode(depth+1, parentKey, body, n, cl)
}
if !oneline {
// if it's not a single-line list, add a trailing comma
body.AppendUnstructuredTokens(hclv2write.Tokens{tokComma, tokNewline})
}
body.AppendUnstructuredTokens(hclv2write.Tokens{tokCBracket})
case *hclv1ast.LiteralType:
if nv.LeadComment != nil {
c.writeNode(depth, parentKey, body, nv.LeadComment, nil)
}
c.writeLiteral(body, nv)
if nv.LineComment != nil {
c.writeNode(depth, parentKey, body, nv.LineComment, nil)
}
case *hclv1ast.ObjectItem:
if nv.LeadComment != nil {
c.writeNode(depth, parentKey, body, nv.LeadComment, nil)
}
key := nv.Keys[0].Token.Text
tok := hclv2write.Tokens{
{Type: hclv2syntax.TokenIdent, Bytes: []byte(key)},
}
if !isBlock(key, depth, parentKey) {
tok = append(tok, tokEqual)
} else if len(nv.Keys) > 1 {
for _, k := range nv.Keys[1:] {
kv := k.Token.Value().(string)
tok = append(tok, tokOQuote, &hclv2write.Token{Type: hclv2syntax.TokenQuotedLit, Bytes: []byte(kv)}, tokCQuote)
}
}
body.AppendUnstructuredTokens(tok)
c.writeNode(depth, key, body, nv.Val, cl)
if nv.LineComment != nil {
c.writeNode(depth, parentKey, body, nv.LineComment, nil)
}
body.AppendNewline()
case *hclv1ast.ObjectList:
for i, item := range nv.Items {
if i > 0 && needNewline(item, nv.Items[i-1], cl) {
body.AppendNewline()
}
c.writeNode(depth+1, parentKey, body, item, cl)
}
case *hclv1ast.ObjectType:
body.AppendUnstructuredTokens(hclv2write.Tokens{tokOBrace, tokNewline})
c.writeNode(depth, parentKey, body, nv.List, cl)
body.AppendUnstructuredTokens(hclv2write.Tokens{tokCBrace})
case *hclv1ast.CommentGroup:
for _, c := range nv.List {
body.AppendUnstructuredTokens(hclv2write.Tokens{
tokComment(c.Text),
tokNewline,
})
}
}
}
func (c *command) writeComments(body *hclv2write.Body, comments commentList) {
if len(comments) == 0 {
return
}
for _, cg := range comments {
if c := cg.List[0]; c.Start.Line != 1 {
// Don't prepend a newline if this comment is the first thing in the file
body.AppendNewline()
}
for _, c := range cg.List {
body.AppendUnstructuredTokens(hclv2write.Tokens{
{
Type: hclv2syntax.TokenComment,
Bytes: []byte(c.Text),
},
tokNewline,
})
}
}
body.AppendNewline()
}
func (c *command) writeLiteral(body *hclv2write.Body, val *hclv1ast.LiteralType) {
switch val.Token.Type {
case hclv1token.NUMBER, hclv1token.FLOAT:
body.AppendUnstructuredTokens(hclv2write.Tokens{
{
Type: hclv2syntax.TokenNumberLit,
Bytes: []byte(val.Token.Text),
},
})
case hclv1token.BOOL:
body.AppendUnstructuredTokens(hclv2write.Tokens{
{
Type: hclv2syntax.TokenIdent,
Bytes: []byte(val.Token.Text),
},
})
case hclv1token.HEREDOC:
// TODO: a quick look at the terraform 0.12upgrade command indicates
// that this may not be sufficient. I should probably insert a TODO
// into the upgraded configuration to check any upgraded heredocs.
// This is good enough for now though.
newlineIdx := strings.IndexByte(val.Token.Text, '\n')
if newlineIdx < 0 {
panic("invalid heredoc")
}
// start from 2; don't include <<
delim := val.Token.Text[2 : newlineIdx+1]
if delim[0] == '-' {
delim = delim[1:]
}
body.AppendUnstructuredTokens(hclv2write.Tokens{
{
Type: hclv2syntax.TokenOHeredoc,
Bytes: append([]byte("<<"), []byte(delim)...),
},
{
Type: hclv2syntax.TokenStringLit,
Bytes: []byte(val.Token.Value().(string)),
},
{
Type: hclv2syntax.TokenCHeredoc,
Bytes: []byte(delim),
},
})
case hclv1token.STRING:
tmpTok := upgradeExpr(val.Token.Text)
// convert from hclsyntax.Tokens to hclwrite.Tokens
var tok hclv2write.Tokens
for _, t := range tmpTok {
tok = append(tok, &hclv2write.Token{
Type: t.Type,
Bytes: t.Bytes,
})
}
upgradeFunctionNames(tok)
body.AppendUnstructuredTokens(tok)
}
}
// loadDetachedComments returns comments that are not associated with a node as either
// a lead comment or a line comment.
func (c *command) loadDetachedComments(f *hclv1ast.File) *commentList {
var (
ret commentList
m = make(map[hclv1token.Pos]*hclv1ast.CommentGroup)
)
hclv1ast.Walk(f, func(n hclv1ast.Node) (hclv1ast.Node, bool) {
switch val := n.(type) {
case *hclv1ast.ObjectItem:
if val.LeadComment != nil {
m[val.LeadComment.Pos()] = val.LeadComment
}
if val.LineComment != nil {
m[val.LineComment.Pos()] = val.LineComment
}
case *hclv1ast.LiteralType:
if val.LeadComment != nil {
m[val.LeadComment.Pos()] = val.LeadComment
}
if val.LineComment != nil {
m[val.LineComment.Pos()] = val.LineComment
}
}
return n, true
})
for _, c := range f.Comments {
c := c
if _, ok := m[c.Pos()]; !ok {
ret = append(ret, c)
}
}
return &ret
}
func (c *command) save(path string, contents []byte) error {
// check the new config
p := hclv2parse.NewParser()
_, diags := p.ParseHCL(contents, path)
if diags.HasErrors() {
var d tfdiags.Diagnostics
d = d.Append(diags)
return d.Err()
}
if c.dryRun {
fmt.Printf("%s:\n%s\n", path, contents)
return nil
} else if path == "-" {
os.Stdout.Write(contents)
return nil
}
base := filepath.Dir(path)
newPath := filepath.Join(base, "terragrunt.hcl")
if c.gitMv {
// update the source file and git mv it
err := ioutil.WriteFile(path, contents, 0644)
if err != nil {
return err
}
fmt.Printf("Updated %s\n", path)
cmd := exec.Command("git", "mv", path, newPath)
if err := cmd.Run(); err != nil {
return err
}
} else {
err := ioutil.WriteFile(newPath, contents, 0644)
if err != nil {
return err
}
fmt.Printf("Updated %s\n", path)
if !c.keepOld {
return os.Remove(path)
}
}
return nil
}
type commentList []*hclv1ast.CommentGroup
func (cl *commentList) Len() int {
return len(*cl)
}
func (cl *commentList) PeekBefore(pos hclv1token.Pos) commentList {
var i int
for i = 0; i < len(*cl); i++ {
if (*cl)[i].Pos().After(pos) {
break
}
}
if i == 0 {
return nil
}
return (*cl)[:i]
}
func (cl *commentList) PopBefore(pos hclv1token.Pos) commentList {
var (
ret commentList
i int
)
for i = 0; i < len(*cl); i++ {
if (*cl)[i].Pos().After(pos) {
break
}
}
if i == 0 {
return nil
}
ret = (*cl)[:i]
*cl = (*cl)[i:]
return ret
}
func tokComment(text string) *hclv2write.Token {
return &hclv2write.Token{
Type: hclv2syntax.TokenComment,
Bytes: []byte(text),
}
}
var topLevelBlocks = []string{"terraform", "remote_state", "include", "dependencies"}
// isBlock returns a boolean indicating whether the node identified by
// key at the given depth under the specified parent should be a block.
// If this returns false, the node should be an attribute.
func isBlock(key string, depth int, parent string) bool {
if depth == 0 {
for _, k := range topLevelBlocks {
if key == k {
return true
}
}
} else if depth == 1 && parent == "terraform" && key == "extra_arguments" {
return true
}
return false
}
// needNewline returns true if an extra newline is needed between
// nodes. These cases include:
// - The current node has a leading comment
// - The current or previous node is an object
// - The current or previous node is a multiline list
// But not if:
// - The previous element had a line comment
// - There is a detached comment after the previous node
func needNewline(curr, prev *hclv1ast.ObjectItem, cl *commentList) bool {
if prev.LineComment != nil {
// The previous line comment includes a newline
return false
} else if c := cl.PeekBefore(curr.Pos()); len(c) > 0 {
// The previous detached comment includes a newline
return false
} else if curr.LeadComment != nil {
return true
}
switch v := curr.Val.(type) {
case *hclv1ast.LiteralType:
if v.LeadComment != nil {
return true
}
case *hclv1ast.ListType:
if v.Lbrack.Line != v.Rbrack.Line {
return true
}
case *hclv1ast.ObjectType:
return true
}
switch v := prev.Val.(type) {
case *hclv1ast.ListType:
if v.Lbrack.Line != v.Rbrack.Line {
return true
}
case *hclv1ast.ObjectType:
return true
}
return false
}
func upgradeExpr(expr string) hclv2syntax.Tokens {
tok, diag := hclv2syntax.LexExpression([]byte(expr), "", hclv2.Pos{})
if diag.HasErrors() {
// TODO: should probably do something about this.
return tok
}
if tok[len(tok)-1].Type == hclv2syntax.TokenEOF {
tok = tok[:len(tok)-1]
}
if len(tok) < 5 {
// Not enough tokens for an interpolation (open quote, start template (${), inner token(s), close template (}), close quote)
return tok
}
oq := tok[0]
ot := tok[1]
ct := tok[len(tok)-2]
cq := tok[len(tok)-1]
inner := tok[2 : len(tok)-2]
if oq.Type != hclv2syntax.TokenOQuote || ot.Type != hclv2syntax.TokenTemplateInterp || ct.Type != hclv2syntax.TokenTemplateSeqEnd || cq.Type != hclv2syntax.TokenCQuote {
// Not an intepolation that looks like "${expr}"
return tok
}
quotes := 0
for _, t := range inner {
if t.Type == hclv2syntax.TokenOQuote {
quotes++
continue
}
if t.Type == hclv2syntax.TokenCQuote {
quotes--
continue
}
if quotes > 0 {
// Nested interpolations are ok
continue
}
if t.Type == hclv2syntax.TokenTemplateInterp {
// Interpolation outside of a string, e.g., ${expr1}${expr2}
return tok
}
}
// Return the tokens without the ${}
return inner
}
var renameFuncs = map[string]string{
"get_tfvars_dir": "get_terragrunt_dir",
"get_parent_tfvars_dir": "get_parent_terragrunt_dir",
}
func upgradeFunctionNames(tokens hclv2write.Tokens) {
for i, t := range tokens {
if t.Type == hclv2syntax.TokenIdent {
newName, ok := renameFuncs[string(t.Bytes)]
if !ok {
continue
}
if i+2 >= len(tokens)-1 {
// need at least 2 more tokens in the expresion, '(' and ')', for this to be a valid function call
// since we don't have enough, continue
continue
}
// finally, make sure the next 2 tokens actually _are_ '(' and ')' - since the 2
// renamed functions don't accept any arguments
if tokens[i+1].Type != hclv2syntax.TokenOParen || tokens[i+2].Type != hclv2syntax.TokenCParen {
continue
}
t.Bytes = []byte(newName)
}
}
}