Skip to content

Commit

Permalink
Fix crash when passed --only with a configured rule (#1106)
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 committed Apr 25, 2021
1 parent 022359e commit ca3e503
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 22 deletions.
5 changes: 5 additions & 0 deletions cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ func (cli *CLI) inspect(opts Options, dir string, filterFiles []string) int {
cli.formatter.Print(tflint.Issues{}, tflint.NewContextError("Failed to load TFLint config", err), map[string][]byte{})
return ExitCodeError
}
if len(opts.Only) > 0 {
for _, rule := range cfg.Rules {
rule.Enabled = false
}
}
cfg = cfg.Merge(opts.toConfig())

// Setup loader
Expand Down
5 changes: 5 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ func (cli *CLI) printVersion(opts Options) int {
log.Printf("[ERROR] Failed to load TFLint config: %s", err)
return ExitCodeOK
}
if len(opts.Only) > 0 {
for _, rule := range cfg.Rules {
rule.Enabled = false
}
}
cfg = cfg.Merge(opts.toConfig())

cli.loader, err = tflint.NewLoader(afero.Afero{Fs: afero.NewOsFs()}, cfg)
Expand Down
5 changes: 5 additions & 0 deletions integrationtest/bundled/bundled_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ func TestBundledPlugin(t *testing.T) {
Command: "tflint --enable-rule aws_s3_bucket_name --format json --force",
Dir: "rule-config",
},
{
Name: "rule config with --only",
Command: "tflint --only aws_s3_bucket_name --format json --force",
Dir: "rule-config",
},
}

dir, _ := os.Getwd()
Expand Down
5 changes: 5 additions & 0 deletions langserver/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ func NewHandler(configPath string, cliConfig *tflint.Config) (jsonrpc2.Handler,
if err != nil {
return nil, nil, err
}
if cliConfig.DisabledByDefault {
for _, rule := range cfg.Rules {
rule.Enabled = false
}
}
cfg = cfg.Merge(cliConfig)

// AWS plugin is automatically enabled for the backward compatibility.
Expand Down
23 changes: 2 additions & 21 deletions tflint/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (c *Config) Merge(other *Config) *Config {
ret.Varfiles = append(ret.Varfiles, other.Varfiles...)
ret.Variables = append(ret.Variables, other.Variables...)

ret.Rules = mergeRuleMap(ret.Rules, other.Rules, other.DisabledByDefault)
ret.Rules = mergeRuleMap(ret.Rules, other.Rules)
ret.Plugins = mergePluginMap(ret.Plugins, other.Plugins)

return ret
Expand Down Expand Up @@ -332,27 +332,8 @@ func mergeBoolMap(a, b map[string]bool) map[string]bool {
return ret
}

func mergeRuleMap(a, b map[string]*RuleConfig, bDisabledByDefault bool) map[string]*RuleConfig {
func mergeRuleMap(a, b map[string]*RuleConfig) map[string]*RuleConfig {
ret := map[string]*RuleConfig{}
if bDisabledByDefault {
for bK, bV := range b {
configRuleFound := false
for aK, aV := range a {
if aK == bK {
ret[bK] = bV
ret[bK].Body = aV.Body
ret[bK].Enabled = true
configRuleFound = true
}
}
if !configRuleFound {
ret[bK] = bV
ret[bK].Enabled = true
}
}
return ret
}

for k, v := range a {
ret[k] = v
}
Expand Down
7 changes: 6 additions & 1 deletion tflint/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ func Test_Merge(t *testing.T) {
},
"aws_instance_invalid_ami": {
Name: "aws_instance_invalid_ami",
Enabled: true,
Enabled: false,
Body: file1.Body,
},
},
Expand Down Expand Up @@ -455,6 +455,11 @@ func Test_Merge(t *testing.T) {
Enabled: true,
Body: hcl.EmptyBody(),
},
"aws_instance_invalid_ami": {
Name: "aws_instance_invalid_ami",
Enabled: false,
Body: file1.Body,
},
},
Plugins: map[string]*PluginConfig{
"foo": {
Expand Down

0 comments on commit ca3e503

Please sign in to comment.