diff --git a/README.md b/README.md index d7d0b131d..04c34e96e 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ plugin "terraform" { } ``` -See the [tflint-ruleset-terraform documentation](https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/configuration.md) for more information. +See the [tflint-ruleset-terraform documentation](https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.1/docs/configuration.md) for more information. 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: diff --git a/cmd/inspect.go b/cmd/inspect.go index 59fa381a5..6b709ff00 100644 --- a/cmd/inspect.go +++ b/cmd/inspect.go @@ -17,6 +17,8 @@ func (cli *CLI) inspect(opts Options, dir string, filterFiles []string) int { cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to load TFLint config; %w", err), map[string][]byte{}) return ExitCodeError } + // tflint-plugin-sdk v0.13+ doesn't need to disable rules config when enabling the only option. + // This is for the backward compatibility. if len(opts.Only) > 0 { for _, rule := range cfg.Rules { rule.Enabled = false diff --git a/cmd/option.go b/cmd/option.go index b6668f196..d88f98b76 100644 --- a/cmd/option.go +++ b/cmd/option.go @@ -63,6 +63,8 @@ func (opts *Options) toConfig() *tflint.Config { rules := map[string]*tflint.RuleConfig{} if len(opts.Only) > 0 { + // tflint-plugin-sdk v0.13+ doesn't need rules config when enabling the only option. + // This is for the backward compatibility. for _, rule := range opts.Only { rules[rule] = &tflint.RuleConfig{ Name: rule, @@ -107,6 +109,7 @@ func (opts *Options) toConfig() *tflint.Config { Varfiles: varfiles, Variables: opts.Variables, DisabledByDefault: len(opts.Only) > 0, + Only: opts.Only, Format: opts.Format, Rules: rules, Plugins: plugins, diff --git a/cmd/option_test.go b/cmd/option_test.go index 3bbd0eea8..fafd34332 100644 --- a/cmd/option_test.go +++ b/cmd/option_test.go @@ -179,6 +179,7 @@ func Test_toConfig(t *testing.T) { Varfiles: []string{}, Variables: []string{}, DisabledByDefault: true, + Only: []string{"aws_instance_invalid_type"}, Rules: map[string]*tflint.RuleConfig{ "aws_instance_invalid_type": { Name: "aws_instance_invalid_type", diff --git a/docs/user-guide/config.md b/docs/user-guide/config.md index 13d5873b9..6efa340d5 100644 --- a/docs/user-guide/config.md +++ b/docs/user-guide/config.md @@ -170,3 +170,13 @@ Some rules support additional attributes that configure their behavior. See the ### `plugin` blocks You can declare the plugin to use. See [Configuring Plugins](plugins.md) + +## Rule config priority + +The priority of rule configs is as follows: + +1. `--only` (CLI flag) +2. `--enable-rule`, `--disable-rule` (CLI flag) +3. `rule` blocks (config file) +4. `preset` (config file, tflint-ruleset-terraform only) +5. `disabled_by_default` (config file) diff --git a/go.mod b/go.mod index 80ad8754d..c88bb5908 100644 --- a/go.mod +++ b/go.mod @@ -24,8 +24,8 @@ require ( github.com/sourcegraph/go-lsp v0.0.0-20200429204803-219e11d77f5d github.com/sourcegraph/jsonrpc2 v0.1.0 github.com/spf13/afero v1.9.2 - github.com/terraform-linters/tflint-plugin-sdk v0.12.0 - github.com/terraform-linters/tflint-ruleset-terraform v0.1.0 + github.com/terraform-linters/tflint-plugin-sdk v0.13.0 + github.com/terraform-linters/tflint-ruleset-terraform v0.1.1 github.com/xeipuuv/gojsonschema v1.2.0 github.com/zclconf/go-cty v1.11.0 github.com/zclconf/go-cty-yaml v1.0.2 diff --git a/go.sum b/go.sum index ca41f88a3..169bc4f8e 100644 --- a/go.sum +++ b/go.sum @@ -260,10 +260,10 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5 github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s= github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals= -github.com/terraform-linters/tflint-plugin-sdk v0.12.0 h1:M++ZPQXIbcGKnI6KyQz3AblCkodoKIY/glheL43LTOU= -github.com/terraform-linters/tflint-plugin-sdk v0.12.0/go.mod h1:/lXvT/LfKOWdjoQgtRbA8VSHtGr8RFwarPIDf20lXbc= -github.com/terraform-linters/tflint-ruleset-terraform v0.1.0 h1:Hh0PMunRHT3aU78av2Yu8Movn/cvmICpem9L1B+UP9I= -github.com/terraform-linters/tflint-ruleset-terraform v0.1.0/go.mod h1:LxGNcQPzMptvgMApDj2xJsvGUcALnGZc45WiRs68dYM= +github.com/terraform-linters/tflint-plugin-sdk v0.13.0 h1:aGRUMlN+m1dAGZ7vLPRrC71Esl8GMxC71Fce/RRv3i4= +github.com/terraform-linters/tflint-plugin-sdk v0.13.0/go.mod h1:BWxrWRnzU+wQddNnsY4HGDEVBNLhVzQZZU8tCoRYxVI= +github.com/terraform-linters/tflint-ruleset-terraform v0.1.1 h1:dAi25kUMZ3+c1aiQZlP+ifxXnRv+WNpSVSMBroUxeOI= +github.com/terraform-linters/tflint-ruleset-terraform v0.1.1/go.mod h1:+iOphcKeOXXNPqjc3STxHvJ+4km5y8Zo+qqqPLgqFFw= github.com/ulikunitz/xz v0.5.8 h1:ERv8V6GKqVi23rgu5cj9pVfVzJbOqAY2Ntl88O6c2nQ= github.com/ulikunitz/xz v0.5.8/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= diff --git a/integrationtest/bundled/basic/result.json b/integrationtest/bundled/basic/result.json index cba9858ee..a176b0333 100644 --- a/integrationtest/bundled/basic/result.json +++ b/integrationtest/bundled/basic/result.json @@ -4,7 +4,7 @@ "rule": { "name": "terraform_required_version", "severity": "warning", - "link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_required_version.md" + "link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.1/docs/rules/terraform_required_version.md" }, "message": "terraform \"required_version\" attribute is required", "range": { @@ -24,7 +24,7 @@ "rule": { "name": "terraform_typed_variables", "severity": "warning", - "link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_typed_variables.md" + "link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.1/docs/rules/terraform_typed_variables.md" }, "message": "`instance_type` variable has no type", "range": { @@ -44,7 +44,7 @@ "rule": { "name": "terraform_unused_declarations", "severity": "warning", - "link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_unused_declarations.md" + "link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.1/docs/rules/terraform_unused_declarations.md" }, "message": "variable \"unused\" is declared but not used", "range": { @@ -64,7 +64,7 @@ "rule": { "name": "terraform_required_providers", "severity": "warning", - "link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_required_providers.md" + "link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.1/docs/rules/terraform_required_providers.md" }, "message": "Missing version constraint for provider \"aws\" in \"required_providers\"", "range": { @@ -84,7 +84,7 @@ "rule": { "name": "terraform_empty_list_equality", "severity": "warning", - "link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_empty_list_equality.md" + "link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.1/docs/rules/terraform_empty_list_equality.md" }, "message": "Comparing a collection with an empty list is invalid. To detect an empty collection, check its length.", "range": { @@ -104,7 +104,7 @@ "rule": { "name": "terraform_deprecated_interpolation", "severity": "warning", - "link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_deprecated_interpolation.md" + "link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.1/docs/rules/terraform_deprecated_interpolation.md" }, "message": "Interpolation-only expressions are deprecated in Terraform v0.12.14", "range": { diff --git a/integrationtest/bundled/bundled_test.go b/integrationtest/bundled/bundled_test.go index 99f4522a4..c29d79e41 100644 --- a/integrationtest/bundled/bundled_test.go +++ b/integrationtest/bundled/bundled_test.go @@ -46,6 +46,11 @@ func TestIntegration(t *testing.T) { command: "tflint --format json --force", dir: "disabled_by_default", }, + { + name: "only", + command: "tflint --format json --force --only terraform_unused_declarations", + dir: "only", + }, } dir, _ := os.Getwd() diff --git a/integrationtest/bundled/disabled_by_default/result.json b/integrationtest/bundled/disabled_by_default/result.json index 0f0bbaf89..6fe7ea0b3 100644 --- a/integrationtest/bundled/disabled_by_default/result.json +++ b/integrationtest/bundled/disabled_by_default/result.json @@ -4,7 +4,7 @@ "rule": { "name": "terraform_unused_declarations", "severity": "warning", - "link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_unused_declarations.md" + "link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.1/docs/rules/terraform_unused_declarations.md" }, "message": "variable \"unused\" is declared but not used", "range": { diff --git a/integrationtest/bundled/only/main.tf b/integrationtest/bundled/only/main.tf new file mode 100644 index 000000000..8b615e7bf --- /dev/null +++ b/integrationtest/bundled/only/main.tf @@ -0,0 +1,6 @@ +variable "unused" {} +variable "used" {} + +resource "aws_instance" "main" { + instance_type = "${var.used}" +} diff --git a/integrationtest/bundled/only/result.json b/integrationtest/bundled/only/result.json new file mode 100644 index 000000000..6fe7ea0b3 --- /dev/null +++ b/integrationtest/bundled/only/result.json @@ -0,0 +1,25 @@ +{ + "issues": [ + { + "rule": { + "name": "terraform_unused_declarations", + "severity": "warning", + "link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.1/docs/rules/terraform_unused_declarations.md" + }, + "message": "variable \"unused\" is declared but not used", + "range": { + "filename": "main.tf", + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "callers": [] + } + ], + "errors": [] +} diff --git a/integrationtest/bundled/with_config/result.json b/integrationtest/bundled/with_config/result.json index 39935ee9f..5985d8986 100644 --- a/integrationtest/bundled/with_config/result.json +++ b/integrationtest/bundled/with_config/result.json @@ -4,7 +4,7 @@ "rule": { "name": "terraform_required_version", "severity": "warning", - "link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_required_version.md" + "link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.1/docs/rules/terraform_required_version.md" }, "message": "terraform \"required_version\" attribute is required", "range": { @@ -24,7 +24,7 @@ "rule": { "name": "terraform_documented_variables", "severity": "info", - "link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_documented_variables.md" + "link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.1/docs/rules/terraform_documented_variables.md" }, "message": "`instance_type` variable has no description", "range": { @@ -44,7 +44,7 @@ "rule": { "name": "terraform_unused_declarations", "severity": "warning", - "link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_unused_declarations.md" + "link": "https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.1/docs/rules/terraform_unused_declarations.md" }, "message": "variable \"instance_type\" is declared but not used", "range": { diff --git a/tflint/config.go b/tflint/config.go index a7eb29c64..1325288ef 100644 --- a/tflint/config.go +++ b/tflint/config.go @@ -64,6 +64,7 @@ type Config struct { Varfiles []string Variables []string DisabledByDefault bool + Only []string PluginDir string Format string Rules map[string]*RuleConfig @@ -348,6 +349,7 @@ func (c *Config) Merge(other *Config) { } c.Varfiles = append(c.Varfiles, other.Varfiles...) c.Variables = append(c.Variables, other.Variables...) + c.Only = append(c.Only, other.Only...) for name, rule := range other.Rules { // 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 { cfg := &sdk.Config{ Rules: map[string]*sdk.RuleConfig{}, DisabledByDefault: c.DisabledByDefault, + Only: c.Only, } for _, rule := range c.Rules { cfg.Rules[rule.Name] = &sdk.RuleConfig{ diff --git a/tflint/config_test.go b/tflint/config_test.go index 1f1b71409..47c2ca20e 100644 --- a/tflint/config_test.go +++ b/tflint/config_test.go @@ -542,6 +542,7 @@ func TestMerge(t *testing.T) { Varfiles: []string{"example3.tfvars"}, Variables: []string{"bar=baz"}, DisabledByDefault: true, + Only: []string{"aws_instance_invalid_type", "aws_instance_previous_type"}, Rules: map[string]*RuleConfig{ "aws_instance_invalid_type": { Name: "aws_instance_invalid_type", @@ -576,6 +577,7 @@ func TestMerge(t *testing.T) { Varfiles: []string{"example1.tfvars", "example2.tfvars", "example3.tfvars"}, Variables: []string{"foo=bar", "bar=baz"}, DisabledByDefault: true, + Only: []string{"aws_instance_invalid_type", "aws_instance_previous_type"}, Rules: map[string]*RuleConfig{ "aws_instance_invalid_type": { Name: "aws_instance_invalid_type", @@ -770,6 +772,7 @@ plugin "bar" { if err != nil { t.Fatal(err) } + config.Only = []string{"aws_instance_invalid_ami"} got := config.ToPluginConfig() want := &sdk.Config{ @@ -784,6 +787,7 @@ plugin "bar" { }, }, DisabledByDefault: true, + Only: []string{"aws_instance_invalid_ami"}, } opts := cmp.Options{ cmpopts.IgnoreUnexported(PluginConfig{}),