Skip to content

Commit

Permalink
Prefer --only option over other rules config
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 committed Sep 17, 2022
1 parent 58748b4 commit d44d3c5
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 6 deletions.
2 changes: 2 additions & 0 deletions cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions cmd/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions cmd/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 12 additions & 0 deletions docs/user-guide/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,15 @@ 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)

Note that these priorities can be changed by plugins. However, for the sake of clarity, plugins are encouraged to follow this priority.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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.12.1-0.20220914154953-7a18971d4b19
github.com/terraform-linters/tflint-ruleset-terraform v0.1.1-0.20220915172014-b42e8a9c02a8
github.com/xeipuuv/gojsonschema v1.2.0
github.com/zclconf/go-cty v1.11.0
github.com/zclconf/go-cty-yaml v1.0.2
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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.12.1-0.20220914154953-7a18971d4b19 h1:y2eHc0kOPivLHfaGsn2UkDac7F1lmvVb+c21fIAL4KY=
github.com/terraform-linters/tflint-plugin-sdk v0.12.1-0.20220914154953-7a18971d4b19/go.mod h1:/lXvT/LfKOWdjoQgtRbA8VSHtGr8RFwarPIDf20lXbc=
github.com/terraform-linters/tflint-ruleset-terraform v0.1.1-0.20220915172014-b42e8a9c02a8 h1:KdzEQ+N1V6OtQKeLyZ9gNaxYkbe/wJU2m0W6InG3w3o=
github.com/terraform-linters/tflint-ruleset-terraform v0.1.1-0.20220915172014-b42e8a9c02a8/go.mod h1:PGl4Ja52e+YPB/V4hgntxS5euCiIo31FQVFY7W0JtNY=
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=
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 @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions integrationtest/bundled/only/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
variable "unused" {}
variable "used" {}

resource "aws_instance" "main" {
instance_type = "${var.used}"
}
25 changes: 25 additions & 0 deletions integrationtest/bundled/only/result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"issues": [
{
"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"
},
"message": "variable \"unused\" is declared but not used",
"range": {
"filename": "main.tf",
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 18
}
},
"callers": []
}
],
"errors": []
}
3 changes: 3 additions & 0 deletions tflint/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type Config struct {
Varfiles []string
Variables []string
DisabledByDefault bool
Only []string
PluginDir string
Format string
Rules map[string]*RuleConfig
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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{
Expand Down
4 changes: 4 additions & 0 deletions tflint/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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{
Expand All @@ -784,6 +787,7 @@ plugin "bar" {
},
},
DisabledByDefault: true,
Only: []string{"aws_instance_invalid_ami"},
}
opts := cmp.Options{
cmpopts.IgnoreUnexported(PluginConfig{}),
Expand Down

0 comments on commit d44d3c5

Please sign in to comment.