Skip to content

Commit f9d57c3

Browse files
authored
Prefer --only option over other rules config (#26)
1 parent a5b68ac commit f9d57c3

File tree

4 files changed

+103
-16
lines changed

4 files changed

+103
-16
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ go 1.19
44

55
require (
66
github.com/Masterminds/semver/v3 v3.1.1
7-
github.com/google/go-cmp v0.5.8
7+
github.com/google/go-cmp v0.5.9
88
github.com/hashicorp/go-getter v1.6.2
99
github.com/hashicorp/go-version v1.6.0
1010
github.com/hashicorp/hcl/v2 v2.14.0
1111
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c
12-
github.com/terraform-linters/tflint-plugin-sdk v0.12.0
12+
github.com/terraform-linters/tflint-plugin-sdk v0.13.0
1313
github.com/zclconf/go-cty v1.11.0
1414
)
1515

go.sum

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
9494
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
9595
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
9696
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
97-
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
9897
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
98+
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
99+
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
99100
github.com/google/martian v2.1.0+incompatible h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no=
100101
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
101102
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
@@ -176,8 +177,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
176177
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
177178
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
178179
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
179-
github.com/terraform-linters/tflint-plugin-sdk v0.12.0 h1:M++ZPQXIbcGKnI6KyQz3AblCkodoKIY/glheL43LTOU=
180-
github.com/terraform-linters/tflint-plugin-sdk v0.12.0/go.mod h1:/lXvT/LfKOWdjoQgtRbA8VSHtGr8RFwarPIDf20lXbc=
180+
github.com/terraform-linters/tflint-plugin-sdk v0.13.0 h1:aGRUMlN+m1dAGZ7vLPRrC71Esl8GMxC71Fce/RRv3i4=
181+
github.com/terraform-linters/tflint-plugin-sdk v0.13.0/go.mod h1:BWxrWRnzU+wQddNnsY4HGDEVBNLhVzQZZU8tCoRYxVI=
181182
github.com/ulikunitz/xz v0.5.8 h1:ERv8V6GKqVi23rgu5cj9pVfVzJbOqAY2Ntl88O6c2nQ=
182183
github.com/ulikunitz/xz v0.5.8/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
183184
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=

terraform/ruleset.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ func (r *RuleSet) ApplyGlobalConfig(config *tflint.Config) error {
4343
// ApplyConfig controls rule activation based on global and preset configs.
4444
// The priority of rules is in the following order:
4545
//
46-
// 1. Rule config declared in each "rule" block
47-
// 2. Preset config declared in "plugin" block
48-
// 3. The `disabled_by_default` declared in global "config" block
46+
// 1. --only option
47+
// 2. Rule config declared in each "rule" block
48+
// 3. Preset config declared in "plugin" block
49+
// 4. The `disabled_by_default` declared in global "config" block
4950
//
5051
// Individual rule configs always take precedence over anything else.
5152
// Preset rules are then prioritized. For example, if `disabled_by_default = true`
@@ -56,12 +57,17 @@ func (r *RuleSet) ApplyConfig(body *hclext.BodyContent) error {
5657
return diags
5758
}
5859

59-
if r.globalConfig.DisabledByDefault {
60+
only := map[string]bool{}
61+
if len(r.globalConfig.Only) > 0 {
6062
logger.Debug("Only mode is enabled. Ignoring default plugin rules")
63+
for _, rule := range r.globalConfig.Only {
64+
only[rule] = true
65+
}
66+
} else if r.globalConfig.DisabledByDefault {
67+
logger.Debug("Default plugin rules are disabled by default")
6168
}
6269

63-
// Default preset is "all"
64-
rules := r.PresetRules["all"]
70+
preset := map[string]bool{}
6571
_, presetExists := body.Attributes["preset"]
6672
if presetExists {
6773
presetRules, exists := r.PresetRules[r.rulesetConfig.Preset]
@@ -72,16 +78,24 @@ func (r *RuleSet) ApplyConfig(body *hclext.BodyContent) error {
7278
}
7379
return fmt.Errorf(`preset "%s" is not found. Valid presets are %s`, r.rulesetConfig.Preset, strings.Join(validPresets, ", "))
7480
}
75-
rules = presetRules
81+
for _, rule := range presetRules {
82+
preset[rule.Name()] = true
83+
}
7684
}
7785

7886
r.EnabledRules = []tflint.Rule{}
79-
for _, rule := range rules {
87+
for _, rule := range r.PresetRules["all"] {
8088
enabled := rule.Enabled()
81-
if cfg := r.globalConfig.Rules[rule.Name()]; cfg != nil {
89+
if len(only) > 0 {
90+
enabled = only[rule.Name()]
91+
} else if cfg := r.globalConfig.Rules[rule.Name()]; cfg != nil {
8292
enabled = cfg.Enabled
83-
} else if r.globalConfig.DisabledByDefault && !presetExists {
84-
// Preset takes precedence over DisabledByDefault
93+
} else if presetExists {
94+
// Ignore rules not in preset
95+
if !preset[rule.Name()] {
96+
enabled = false
97+
}
98+
} else if r.globalConfig.DisabledByDefault {
8599
enabled = false
86100
}
87101

terraform/ruleset_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ func TestApplyConfig(t *testing.T) {
9292
"terraform_deprecated_interpolation",
9393
},
9494
},
95+
{
96+
name: "only",
97+
global: &tflint.Config{Only: []string{"terraform_deprecated_interpolation"}},
98+
config: &hclext.BodyContent{},
99+
want: []string{
100+
"terraform_deprecated_interpolation",
101+
},
102+
},
95103
{
96104
name: "disabled by default + preset",
97105
global: &tflint.Config{DisabledByDefault: true},
@@ -121,6 +129,17 @@ func TestApplyConfig(t *testing.T) {
121129
"terraform_comment_syntax",
122130
},
123131
},
132+
{
133+
name: "disabled by default + only",
134+
global: &tflint.Config{
135+
DisabledByDefault: true,
136+
Only: []string{"terraform_comment_syntax"},
137+
},
138+
config: &hclext.BodyContent{},
139+
want: []string{
140+
"terraform_comment_syntax",
141+
},
142+
},
124143
{
125144
name: "preset + rule config",
126145
global: &tflint.Config{
@@ -140,6 +159,37 @@ func TestApplyConfig(t *testing.T) {
140159
"terraform_deprecated_index",
141160
},
142161
},
162+
{
163+
name: "preset + only",
164+
global: &tflint.Config{
165+
Only: []string{"terraform_deprecated_interpolation"},
166+
},
167+
config: &hclext.BodyContent{
168+
Attributes: hclext.Attributes{
169+
"preset": &hclext.Attribute{Name: "preset", Expr: mustParseExpr(`"recommended"`)},
170+
},
171+
},
172+
want: []string{
173+
"terraform_deprecated_interpolation",
174+
},
175+
},
176+
{
177+
name: "rule config + only",
178+
global: &tflint.Config{
179+
Rules: map[string]*tflint.RuleConfig{
180+
"terraform_comment_syntax": {
181+
Name: "terraform_comment_syntax",
182+
Enabled: false,
183+
},
184+
},
185+
Only: []string{"terraform_comment_syntax", "terraform_deprecated_interpolation"},
186+
},
187+
config: &hclext.BodyContent{},
188+
want: []string{
189+
"terraform_comment_syntax",
190+
"terraform_deprecated_interpolation",
191+
},
192+
},
143193
{
144194
name: "disabled by default + preset + rule config",
145195
global: &tflint.Config{
@@ -160,6 +210,28 @@ func TestApplyConfig(t *testing.T) {
160210
"terraform_deprecated_index",
161211
},
162212
},
213+
{
214+
name: "disabled by default + preset + rule config + only",
215+
global: &tflint.Config{
216+
Rules: map[string]*tflint.RuleConfig{
217+
"terraform_comment_syntax": {
218+
Name: "terraform_comment_syntax",
219+
Enabled: false,
220+
},
221+
},
222+
DisabledByDefault: true,
223+
Only: []string{"terraform_comment_syntax", "terraform_deprecated_interpolation"},
224+
},
225+
config: &hclext.BodyContent{
226+
Attributes: hclext.Attributes{
227+
"preset": &hclext.Attribute{Name: "preset", Expr: mustParseExpr(`"recommended"`)},
228+
},
229+
},
230+
want: []string{
231+
"terraform_comment_syntax",
232+
"terraform_deprecated_interpolation",
233+
},
234+
},
163235
}
164236

165237
for _, test := range tests {

0 commit comments

Comments
 (0)