Skip to content

Commit 3922522

Browse files
authored
Prefer --only option over other rules config (#1516)
1 parent 58748b4 commit 3922522

File tree

15 files changed

+76
-17
lines changed

15 files changed

+76
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ plugin "terraform" {
8585
}
8686
```
8787

88-
See the [tflint-ruleset-terraform documentation](https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/configuration.md) for more information.
88+
See the [tflint-ruleset-terraform documentation](https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.1/docs/configuration.md) for more information.
8989

9090
Next, If you are using an AWS/Azure/GCP provider, it is a good idea to install the plugin and try it according to each usage:
9191

cmd/inspect.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ func (cli *CLI) inspect(opts Options, dir string, filterFiles []string) int {
1717
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to load TFLint config; %w", err), map[string][]byte{})
1818
return ExitCodeError
1919
}
20+
// tflint-plugin-sdk v0.13+ doesn't need to disable rules config when enabling the only option.
21+
// This is for the backward compatibility.
2022
if len(opts.Only) > 0 {
2123
for _, rule := range cfg.Rules {
2224
rule.Enabled = false

cmd/option.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ func (opts *Options) toConfig() *tflint.Config {
6363

6464
rules := map[string]*tflint.RuleConfig{}
6565
if len(opts.Only) > 0 {
66+
// tflint-plugin-sdk v0.13+ doesn't need rules config when enabling the only option.
67+
// This is for the backward compatibility.
6668
for _, rule := range opts.Only {
6769
rules[rule] = &tflint.RuleConfig{
6870
Name: rule,
@@ -107,6 +109,7 @@ func (opts *Options) toConfig() *tflint.Config {
107109
Varfiles: varfiles,
108110
Variables: opts.Variables,
109111
DisabledByDefault: len(opts.Only) > 0,
112+
Only: opts.Only,
110113
Format: opts.Format,
111114
Rules: rules,
112115
Plugins: plugins,

cmd/option_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ func Test_toConfig(t *testing.T) {
179179
Varfiles: []string{},
180180
Variables: []string{},
181181
DisabledByDefault: true,
182+
Only: []string{"aws_instance_invalid_type"},
182183
Rules: map[string]*tflint.RuleConfig{
183184
"aws_instance_invalid_type": {
184185
Name: "aws_instance_invalid_type",

docs/user-guide/config.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,13 @@ Some rules support additional attributes that configure their behavior. See the
170170
### `plugin` blocks
171171

172172
You can declare the plugin to use. See [Configuring Plugins](plugins.md)
173+
174+
## Rule config priority
175+
176+
The priority of rule configs is as follows:
177+
178+
1. `--only` (CLI flag)
179+
2. `--enable-rule`, `--disable-rule` (CLI flag)
180+
3. `rule` blocks (config file)
181+
4. `preset` (config file, tflint-ruleset-terraform only)
182+
5. `disabled_by_default` (config file)

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ require (
2424
github.com/sourcegraph/go-lsp v0.0.0-20200429204803-219e11d77f5d
2525
github.com/sourcegraph/jsonrpc2 v0.1.0
2626
github.com/spf13/afero v1.9.2
27-
github.com/terraform-linters/tflint-plugin-sdk v0.12.0
28-
github.com/terraform-linters/tflint-ruleset-terraform v0.1.0
27+
github.com/terraform-linters/tflint-plugin-sdk v0.13.0
28+
github.com/terraform-linters/tflint-ruleset-terraform v0.1.1
2929
github.com/xeipuuv/gojsonschema v1.2.0
3030
github.com/zclconf/go-cty v1.11.0
3131
github.com/zclconf/go-cty-yaml v1.0.2

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,10 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
260260
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
261261
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
262262
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
263-
github.com/terraform-linters/tflint-plugin-sdk v0.12.0 h1:M++ZPQXIbcGKnI6KyQz3AblCkodoKIY/glheL43LTOU=
264-
github.com/terraform-linters/tflint-plugin-sdk v0.12.0/go.mod h1:/lXvT/LfKOWdjoQgtRbA8VSHtGr8RFwarPIDf20lXbc=
265-
github.com/terraform-linters/tflint-ruleset-terraform v0.1.0 h1:Hh0PMunRHT3aU78av2Yu8Movn/cvmICpem9L1B+UP9I=
266-
github.com/terraform-linters/tflint-ruleset-terraform v0.1.0/go.mod h1:LxGNcQPzMptvgMApDj2xJsvGUcALnGZc45WiRs68dYM=
263+
github.com/terraform-linters/tflint-plugin-sdk v0.13.0 h1:aGRUMlN+m1dAGZ7vLPRrC71Esl8GMxC71Fce/RRv3i4=
264+
github.com/terraform-linters/tflint-plugin-sdk v0.13.0/go.mod h1:BWxrWRnzU+wQddNnsY4HGDEVBNLhVzQZZU8tCoRYxVI=
265+
github.com/terraform-linters/tflint-ruleset-terraform v0.1.1 h1:dAi25kUMZ3+c1aiQZlP+ifxXnRv+WNpSVSMBroUxeOI=
266+
github.com/terraform-linters/tflint-ruleset-terraform v0.1.1/go.mod h1:+iOphcKeOXXNPqjc3STxHvJ+4km5y8Zo+qqqPLgqFFw=
267267
github.com/ulikunitz/xz v0.5.8 h1:ERv8V6GKqVi23rgu5cj9pVfVzJbOqAY2Ntl88O6c2nQ=
268268
github.com/ulikunitz/xz v0.5.8/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
269269
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=

integrationtest/bundled/basic/result.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"rule": {
55
"name": "terraform_required_version",
66
"severity": "warning",
7-
"link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_required_version.md"
7+
"link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.1/docs/rules/terraform_required_version.md"
88
},
99
"message": "terraform \"required_version\" attribute is required",
1010
"range": {
@@ -24,7 +24,7 @@
2424
"rule": {
2525
"name": "terraform_typed_variables",
2626
"severity": "warning",
27-
"link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_typed_variables.md"
27+
"link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.1/docs/rules/terraform_typed_variables.md"
2828
},
2929
"message": "`instance_type` variable has no type",
3030
"range": {
@@ -44,7 +44,7 @@
4444
"rule": {
4545
"name": "terraform_unused_declarations",
4646
"severity": "warning",
47-
"link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_unused_declarations.md"
47+
"link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.1/docs/rules/terraform_unused_declarations.md"
4848
},
4949
"message": "variable \"unused\" is declared but not used",
5050
"range": {
@@ -64,7 +64,7 @@
6464
"rule": {
6565
"name": "terraform_required_providers",
6666
"severity": "warning",
67-
"link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_required_providers.md"
67+
"link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.1/docs/rules/terraform_required_providers.md"
6868
},
6969
"message": "Missing version constraint for provider \"aws\" in \"required_providers\"",
7070
"range": {
@@ -84,7 +84,7 @@
8484
"rule": {
8585
"name": "terraform_empty_list_equality",
8686
"severity": "warning",
87-
"link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_empty_list_equality.md"
87+
"link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.1/docs/rules/terraform_empty_list_equality.md"
8888
},
8989
"message": "Comparing a collection with an empty list is invalid. To detect an empty collection, check its length.",
9090
"range": {
@@ -104,7 +104,7 @@
104104
"rule": {
105105
"name": "terraform_deprecated_interpolation",
106106
"severity": "warning",
107-
"link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_deprecated_interpolation.md"
107+
"link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.1/docs/rules/terraform_deprecated_interpolation.md"
108108
},
109109
"message": "Interpolation-only expressions are deprecated in Terraform v0.12.14",
110110
"range": {

integrationtest/bundled/bundled_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ func TestIntegration(t *testing.T) {
4646
command: "tflint --format json --force",
4747
dir: "disabled_by_default",
4848
},
49+
{
50+
name: "only",
51+
command: "tflint --format json --force --only terraform_unused_declarations",
52+
dir: "only",
53+
},
4954
}
5055

5156
dir, _ := os.Getwd()

integrationtest/bundled/disabled_by_default/result.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"rule": {
55
"name": "terraform_unused_declarations",
66
"severity": "warning",
7-
"link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_unused_declarations.md"
7+
"link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.1/docs/rules/terraform_unused_declarations.md"
88
},
99
"message": "variable \"unused\" is declared but not used",
1010
"range": {

integrationtest/bundled/only/main.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
variable "unused" {}
2+
variable "used" {}
3+
4+
resource "aws_instance" "main" {
5+
instance_type = "${var.used}"
6+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"issues": [
3+
{
4+
"rule": {
5+
"name": "terraform_unused_declarations",
6+
"severity": "warning",
7+
"link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.1/docs/rules/terraform_unused_declarations.md"
8+
},
9+
"message": "variable \"unused\" is declared but not used",
10+
"range": {
11+
"filename": "main.tf",
12+
"start": {
13+
"line": 1,
14+
"column": 1
15+
},
16+
"end": {
17+
"line": 1,
18+
"column": 18
19+
}
20+
},
21+
"callers": []
22+
}
23+
],
24+
"errors": []
25+
}

integrationtest/bundled/with_config/result.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"rule": {
55
"name": "terraform_required_version",
66
"severity": "warning",
7-
"link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_required_version.md"
7+
"link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.1/docs/rules/terraform_required_version.md"
88
},
99
"message": "terraform \"required_version\" attribute is required",
1010
"range": {
@@ -24,7 +24,7 @@
2424
"rule": {
2525
"name": "terraform_documented_variables",
2626
"severity": "info",
27-
"link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_documented_variables.md"
27+
"link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.1/docs/rules/terraform_documented_variables.md"
2828
},
2929
"message": "`instance_type` variable has no description",
3030
"range": {
@@ -44,7 +44,7 @@
4444
"rule": {
4545
"name": "terraform_unused_declarations",
4646
"severity": "warning",
47-
"link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_unused_declarations.md"
47+
"link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.1/docs/rules/terraform_unused_declarations.md"
4848
},
4949
"message": "variable \"instance_type\" is declared but not used",
5050
"range": {

tflint/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ type Config struct {
6464
Varfiles []string
6565
Variables []string
6666
DisabledByDefault bool
67+
Only []string
6768
PluginDir string
6869
Format string
6970
Rules map[string]*RuleConfig
@@ -348,6 +349,7 @@ func (c *Config) Merge(other *Config) {
348349
}
349350
c.Varfiles = append(c.Varfiles, other.Varfiles...)
350351
c.Variables = append(c.Variables, other.Variables...)
352+
c.Only = append(c.Only, other.Only...)
351353

352354
for name, rule := range other.Rules {
353355
// HACK: If you enable the rule through the CLI instead of the file, its hcl.Body will be nil.
@@ -375,6 +377,7 @@ func (c *Config) ToPluginConfig() *sdk.Config {
375377
cfg := &sdk.Config{
376378
Rules: map[string]*sdk.RuleConfig{},
377379
DisabledByDefault: c.DisabledByDefault,
380+
Only: c.Only,
378381
}
379382
for _, rule := range c.Rules {
380383
cfg.Rules[rule.Name] = &sdk.RuleConfig{

tflint/config_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ func TestMerge(t *testing.T) {
542542
Varfiles: []string{"example3.tfvars"},
543543
Variables: []string{"bar=baz"},
544544
DisabledByDefault: true,
545+
Only: []string{"aws_instance_invalid_type", "aws_instance_previous_type"},
545546
Rules: map[string]*RuleConfig{
546547
"aws_instance_invalid_type": {
547548
Name: "aws_instance_invalid_type",
@@ -576,6 +577,7 @@ func TestMerge(t *testing.T) {
576577
Varfiles: []string{"example1.tfvars", "example2.tfvars", "example3.tfvars"},
577578
Variables: []string{"foo=bar", "bar=baz"},
578579
DisabledByDefault: true,
580+
Only: []string{"aws_instance_invalid_type", "aws_instance_previous_type"},
579581
Rules: map[string]*RuleConfig{
580582
"aws_instance_invalid_type": {
581583
Name: "aws_instance_invalid_type",
@@ -770,6 +772,7 @@ plugin "bar" {
770772
if err != nil {
771773
t.Fatal(err)
772774
}
775+
config.Only = []string{"aws_instance_invalid_ami"}
773776

774777
got := config.ToPluginConfig()
775778
want := &sdk.Config{
@@ -784,6 +787,7 @@ plugin "bar" {
784787
},
785788
},
786789
DisabledByDefault: true,
790+
Only: []string{"aws_instance_invalid_ami"},
787791
}
788792
opts := cmp.Options{
789793
cmpopts.IgnoreUnexported(PluginConfig{}),

0 commit comments

Comments
 (0)