-
Notifications
You must be signed in to change notification settings - Fork 44
/
terraform.go
159 lines (127 loc) · 3.29 KB
/
terraform.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
package terraform
import (
"encoding/json"
"fmt"
"io"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
"github.com/bmatcuk/doublestar"
"github.com/env0/terratag/internal/common"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/thoas/go-funk"
)
func GetResourceType(resource hclwrite.Block) string {
return resource.Labels()[0]
}
func getRootDir(dir string, iacType string) string {
if iacType == string(common.Terragrunt) {
// which mode of terragrunt it is (with or without cache folder).
if _, err := os.Stat(dir + "/.terragrunt-cache"); err != nil {
if os.IsNotExist(err) {
return ""
}
}
return "/.terragrunt-cache"
} else {
return "/.terraform"
}
}
func ValidateInitRun(dir string, iacType string) error {
path := dir + getRootDir(dir, iacType)
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("%s init must run before running terratag", iacType)
}
return fmt.Errorf("couldn't determine if %s init has run: %v", iacType, err)
}
return nil
}
func GetFilePaths(dir string, iacType string) ([]string, error) {
if iacType == string(common.Terragrunt) {
return getTerragruntFilePath(dir)
} else {
return getTerraformFilePaths(dir)
}
}
func getTerragruntFilePath(rootDir string) ([]string, error) {
var tfFiles []string
if err := filepath.WalkDir(rootDir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
log.Printf("[WARN] skipping %s due to an error: %v", path, err)
return filepath.SkipDir
}
if strings.HasSuffix(path, ".tf") {
tfFiles = append(tfFiles, path)
}
return nil
}); err != nil {
return nil, err
}
return tfFiles, nil
}
func getTerraformFilePaths(rootDir string) ([]string, error) {
const tfFileMatcher = "/*.tf"
tfFiles, err := doublestar.Glob(rootDir + tfFileMatcher)
if err != nil {
return nil, err
}
modulesDirs, err := getTerraformModulesDirPaths(rootDir)
if err != nil {
return nil, err
}
for _, moduleDir := range modulesDirs {
matches, err := doublestar.Glob(moduleDir + tfFileMatcher)
if err != nil {
return nil, err
}
tfFiles = append(tfFiles, matches...)
}
for i, tfFile := range tfFiles {
resolvedTfFile, err := filepath.EvalSymlinks(tfFile)
if err != nil {
return nil, err
}
tfFiles[i] = resolvedTfFile
}
return funk.UniqString(tfFiles), nil
}
func getTerraformModulesDirPaths(dir string) ([]string, error) {
var paths []string
var modulesJson ModulesJson
jsonFile, err := os.Open(dir + "/.terraform/modules/modules.json")
//lint:ignore SA5001 not required to check file close status.
defer jsonFile.Close()
if os.IsNotExist(err) {
return paths, nil
}
byteValue, err := io.ReadAll(jsonFile)
if err != nil {
return nil, err
}
if err := json.Unmarshal(byteValue, &modulesJson); err != nil {
return nil, err
}
for _, module := range modulesJson.Modules {
modulePath, err := filepath.EvalSymlinks(dir + "/" + module.Dir)
if os.IsNotExist(err) {
log.Print("[WARN] Module not found, skipping.", dir+"/"+module.Dir)
continue
}
if err != nil {
return nil, err
}
paths = append(paths, modulePath)
}
return paths, nil
}
type ModulesJson struct {
Modules []ModuleMetadata `json:"Modules"`
}
type ModuleMetadata struct {
Key string `json:"Key"`
Source string `json:"Source"`
Dir string `json:"Dir"`
}