forked from terraform-linters/tflint
/
config.go
248 lines (214 loc) · 6.2 KB
/
config.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
package tflint
import (
"fmt"
"log"
"os"
"github.com/hashicorp/hcl2/gohcl"
"github.com/hashicorp/hcl2/hclparse"
homedir "github.com/mitchellh/go-homedir"
"github.com/wata727/tflint/client"
)
var defaultConfigFile = ".tflint.hcl"
var fallbackConfigFile = "~/.tflint.hcl"
type rawConfig struct {
Config *struct {
DeepCheck *bool `hcl:"deep_check"`
AwsCredentials *map[string]string `hcl:"aws_credentials"`
IgnoreModule *map[string]bool `hcl:"ignore_module"`
IgnoreRule *map[string]bool `hcl:"ignore_rule"`
Varfile *[]string `hcl:"varfile"`
TerraformVersion *string `hcl:"terraform_version"`
} `hcl:"config,block"`
Rules []RuleConfig `hcl:"rule,block"`
}
// Config describes the behavior of TFLint
type Config struct {
DeepCheck bool
AwsCredentials client.AwsCredentials
IgnoreModule map[string]bool
IgnoreRule map[string]bool
Varfile []string
TerraformVersion string
Rules map[string]*RuleConfig
}
// RuleConfig is a TFLint's rule config
type RuleConfig struct {
Name string `hcl:"name,label"`
Enabled bool `hcl:"enabled"`
}
// EmptyConfig returns default config
// It is mainly used for testing
func EmptyConfig() *Config {
return &Config{
DeepCheck: false,
AwsCredentials: client.AwsCredentials{},
IgnoreModule: map[string]bool{},
IgnoreRule: map[string]bool{},
Varfile: []string{},
TerraformVersion: "",
Rules: map[string]*RuleConfig{},
}
}
// LoadConfig loads TFLint config from file
// If failed to load the default config file, it tries to load config file under the home directory
// Therefore, if there is no default config file, it will not return an error
func LoadConfig(file string) (*Config, error) {
log.Printf("[INFO] Load config: %s", file)
if _, err := os.Stat(file); !os.IsNotExist(err) {
cfg, err := loadConfigFromFile(file)
if err != nil {
log.Printf("[ERROR] %s", err)
return nil, err
}
return cfg, nil
} else if file != defaultConfigFile {
log.Printf("[ERROR] %s", err)
return nil, fmt.Errorf("`%s` is not found", file)
} else {
log.Printf("[INFO] Default config file is not found. Ignored")
}
fallback, err := homedir.Expand(fallbackConfigFile)
if err != nil {
log.Printf("[ERROR] %s", err)
return nil, err
}
log.Printf("[INFO] Load fallback config: %s", fallback)
if _, err := os.Stat(fallback); !os.IsNotExist(err) {
cfg, err := loadConfigFromFile(fallback)
if err != nil {
return nil, err
}
return cfg, nil
}
log.Printf("[INFO] Fallback config file is not found. Ignored")
log.Print("[INFO] Use default config")
return EmptyConfig(), nil
}
// Merge returns a merged copy of the two configs
// Since the argument takes precedence, it can be used as overwriting of the config
func (c *Config) Merge(other *Config) *Config {
ret := c.copy()
if other.DeepCheck {
ret.DeepCheck = true
}
if other.AwsCredentials.AccessKey != "" {
ret.AwsCredentials.AccessKey = other.AwsCredentials.AccessKey
}
if other.AwsCredentials.SecretKey != "" {
ret.AwsCredentials.SecretKey = other.AwsCredentials.SecretKey
}
if other.AwsCredentials.Profile != "" {
ret.AwsCredentials.Profile = other.AwsCredentials.Profile
}
if other.AwsCredentials.Region != "" {
ret.AwsCredentials.Region = other.AwsCredentials.Region
}
ret.IgnoreModule = mergeBoolMap(ret.IgnoreModule, other.IgnoreModule)
ret.IgnoreRule = mergeBoolMap(ret.IgnoreRule, other.IgnoreRule)
ret.Varfile = append(ret.Varfile, other.Varfile...)
if other.TerraformVersion != "" {
ret.TerraformVersion = other.TerraformVersion
}
ret.Rules = mergeRuleMap(ret.Rules, other.Rules)
return ret
}
func (c *Config) copy() *Config {
ignoreModule := make(map[string]bool)
for k, v := range c.IgnoreModule {
ignoreModule[k] = v
}
ignoreRule := make(map[string]bool)
for k, v := range c.IgnoreRule {
ignoreRule[k] = v
}
varfile := make([]string, len(c.Varfile))
copy(varfile, c.Varfile)
rules := map[string]*RuleConfig{}
for k, v := range c.Rules {
rules[k] = &RuleConfig{}
*rules[k] = *v
}
return &Config{
DeepCheck: c.DeepCheck,
AwsCredentials: c.AwsCredentials,
IgnoreModule: ignoreModule,
IgnoreRule: ignoreRule,
Varfile: varfile,
TerraformVersion: c.TerraformVersion,
Rules: rules,
}
}
func loadConfigFromFile(file string) (*Config, error) {
parser := hclparse.NewParser()
f, diags := parser.ParseHCLFile(file)
if diags.HasErrors() {
return nil, diags
}
var raw rawConfig
diags = gohcl.DecodeBody(f.Body, nil, &raw)
if diags.HasErrors() {
return nil, diags
}
cfg := raw.toConfig()
log.Printf("[DEBUG] Config loaded")
log.Printf("[DEBUG] DeepCheck: %t", cfg.DeepCheck)
log.Printf("[DEBUG] IgnoreModule: %#v", cfg.IgnoreModule)
log.Printf("[DEBUG] IgnoreRule: %#v", cfg.IgnoreRule)
log.Printf("[DEBUG] Varfile: %#v", cfg.Varfile)
log.Printf("[DEBUG] TerraformVersion: %s", cfg.TerraformVersion)
log.Printf("[DEBUG] Rules: %#v", cfg.Rules)
return raw.toConfig(), nil
}
func mergeBoolMap(a, b map[string]bool) map[string]bool {
ret := map[string]bool{}
for k, v := range a {
ret[k] = v
}
for k, v := range b {
ret[k] = v
}
return ret
}
func mergeRuleMap(a, b map[string]*RuleConfig) map[string]*RuleConfig {
ret := map[string]*RuleConfig{}
for k, v := range a {
ret[k] = v
}
for k, v := range b {
ret[k] = v
}
return ret
}
func (raw *rawConfig) toConfig() *Config {
ret := EmptyConfig()
rc := raw.Config
if rc != nil {
if rc.DeepCheck != nil {
ret.DeepCheck = *rc.DeepCheck
}
if rc.AwsCredentials != nil {
credentials := *rc.AwsCredentials
ret.AwsCredentials.AccessKey = credentials["access_key"]
ret.AwsCredentials.SecretKey = credentials["secret_key"]
ret.AwsCredentials.Profile = credentials["profile"]
ret.AwsCredentials.Region = credentials["region"]
}
if rc.IgnoreModule != nil {
ret.IgnoreModule = *rc.IgnoreModule
}
if rc.IgnoreRule != nil {
ret.IgnoreRule = *rc.IgnoreRule
}
if rc.Varfile != nil {
ret.Varfile = *rc.Varfile
}
if rc.TerraformVersion != nil {
ret.TerraformVersion = *rc.TerraformVersion
}
}
for _, r := range raw.Rules {
var rule = r
ret.Rules[rule.Name] = &rule
}
return ret
}