-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
136 lines (123 loc) · 3.15 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
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"github.com/gobwas/glob"
)
var (
appName = "terrafile-ify"
version = "SNAPSHOT"
terrafileVersionDefault = "master"
)
var config struct {
Generate bool
Rewrite bool
Execute bool
Ignore string
ignoreGlob glob.Glob
TerrafileBinary string
PrintVersionAndExit bool
}
func init() {
log.SetFlags(0)
log.SetOutput(os.Stderr)
config.Ignore = ".terraform"
config.TerrafileBinary = "terrafile"
flag.StringVar(&config.Ignore, "ignore", config.Ignore, "ignore files and directories matching this glob pattern")
flag.StringVar(&config.TerrafileBinary, "terrafile-binary", config.TerrafileBinary, "terrafile binary name")
flag.BoolVar(&config.PrintVersionAndExit, "version", config.PrintVersionAndExit, "print version and exit")
flag.Parse()
if config.PrintVersionAndExit {
fmt.Println(version)
os.Exit(0)
}
config.ignoreGlob = glob.MustCompile(config.Ignore, filepath.Separator)
if flag.NArg() != 1 {
log.Printf("one of the commands [generate execute rewrite] must be given")
flag.Usage()
os.Exit(1)
}
switch flag.Arg(0) {
case "generate":
config.Generate = true
case "execute":
config.Execute = true
case "rewrite":
config.Rewrite = true
}
}
func main() {
var scanner scanner
if err := forEachTerraformSourceFileIn(".", scanner.ScanFile); err != nil {
log.Fatal(err)
}
referencesForDir := groupReferencesByDir(scanner.references)
var terrafiles []terrafile
for dir, references := range referencesForDir {
terrafile, err := terrafileForDir(dir, references)
if err != nil {
log.Fatal(fmt.Errorf("%q: v", err))
}
terrafiles = append(terrafiles, *terrafile)
}
if config.Generate {
for _, terrafile := range terrafiles {
log.Printf("generating %q (%d module(s))", terrafile.Path, len(terrafile.Modules))
terrafile.Write()
}
}
if config.Execute {
for _, terrafile := range terrafiles {
log.Printf("executing [terrafile %q]", terrafile.Path)
if err := terrafile.Execute(); err != nil {
log.Fatal(err)
}
}
}
if config.Rewrite {
var rewriter rewriter
replaceByVendored := make(moduleReferenceMap)
paths := make(map[string]bool)
for _, r := range scanner.references {
if r, ok := r.Git(); ok {
replaceByVendored[r.Key()] = r.Vendor()
paths[r.path] = true
}
}
rewriter.replaceBy = replaceByVendored
for path := range paths {
log.Printf("rewriting %q", path)
if err := rewriter.RewriteFile(path); err != nil {
log.Fatal(err)
}
}
}
}
func forEachTerraformSourceFileIn(path string, f func(path string) error) error {
return filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
if config.ignoreGlob.Match(path) {
return filepath.SkipDir
}
if config.ignoreGlob.Match(filepath.Base(path)) {
return filepath.SkipDir
}
}
if filepath.Ext(path) != terraformSourceFileExt {
return nil
}
if config.ignoreGlob.Match(path) {
return nil
}
if config.ignoreGlob.Match(filepath.Base(path)) {
return nil
}
return f(path)
})
}