Skip to content

Commit 3e1d0a5

Browse files
authored
Switch internal Terraform rules to the bundled plugin (#1496)
1 parent c01d5eb commit 3e1d0a5

File tree

84 files changed

+636
-9566
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+636
-9566
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ build:
1313
install:
1414
go install
1515

16-
e2e: prepare
16+
e2e: prepare install
1717
go test -timeout 5m ./integrationtest/inspection ./integrationtest/langserver ./integrationtest/init ./integrationtest/cli
1818

1919
lint:

README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ A Pluggable [Terraform](https://www.terraform.io/) Linter
1212

1313
TFLint is a framework and each feature is provided by plugins, the key features are as follows:
1414

15-
- Find possible errors (like illegal instance types) for Major Cloud providers (AWS/Azure/GCP).
15+
- Find possible errors (like invalid instance types) for Major Cloud providers (AWS/Azure/GCP).
1616
- Warn about deprecated syntax, unused declarations.
1717
- Enforce best practices, naming conventions.
1818

@@ -74,14 +74,25 @@ If you want to run on GitHub Actions, [setup-tflint](https://github.com/terrafor
7474

7575
## Getting Started
7676

77-
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:
77+
First, enable rules for [Terraform Language](https://www.terraform.io/language) (e.g. warn about deprecated syntax, unused declarations). [TFLint Ruleset for Terraform Language](https://github.com/terraform-linters/tflint-ruleset-terraform) is bundled with TFLint, so you can use it without installing it separately.
78+
79+
The bundled plugin enables the "recommended" preset by default, but you can disable the plugin or use a different preset. Declare the plugin block in `.tflint.hcl` like this:
80+
81+
```hcl
82+
plugin "terraform" {
83+
enabled = true
84+
preset = "recommended"
85+
}
86+
```
87+
88+
See the [tflint-ruleset-terraform documentation](https://github.com/terraform-linters/tflint-ruleset-terraform/blob/main/docs/configuration.md) for more information.
89+
90+
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:
7891

7992
- [Amazon Web Services](https://github.com/terraform-linters/tflint-ruleset-aws)
8093
- [Microsoft Azure](https://github.com/terraform-linters/tflint-ruleset-azurerm)
8194
- [Google Cloud Platform](https://github.com/terraform-linters/tflint-ruleset-google)
8295

83-
Rules for the Terraform Language is built into the TFLint binary, so you don't need to install any plugins. Please see [Rules](docs/rules) for a list of available rules.
84-
8596
If you want to extend TFLint with other plugins, you can declare the plugins in the config file and easily install them with `tflint --init`.
8697

8798
```hcl

cmd/bundled_plugin.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/terraform-linters/tflint-plugin-sdk/plugin"
7+
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
8+
"github.com/terraform-linters/tflint-ruleset-terraform/project"
9+
"github.com/terraform-linters/tflint-ruleset-terraform/rules"
10+
"github.com/terraform-linters/tflint-ruleset-terraform/terraform"
11+
)
12+
13+
func (cli *CLI) actAsBundledPlugin() int {
14+
plugin.Serve(&plugin.ServeOpts{
15+
RuleSet: &terraform.RuleSet{
16+
BuiltinRuleSet: tflint.BuiltinRuleSet{
17+
Name: "terraform",
18+
Version: fmt.Sprintf("%s-bundled", project.Version),
19+
},
20+
PresetRules: rules.PresetRules,
21+
},
22+
})
23+
return ExitCodeOK
24+
}

cmd/cli.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ func (cli *CLI) Run(args []string) int {
9797
return cli.init(opts)
9898
case opts.Langserver:
9999
return cli.startLanguageServer(opts.Config, opts.toConfig())
100+
case opts.ActAsBundledPlugin:
101+
return cli.actAsBundledPlugin()
100102
default:
101103
return cli.inspect(opts, dir, filterFiles)
102104
}

cmd/inspect.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/spf13/afero"
88
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
99
"github.com/terraform-linters/tflint/plugin"
10-
"github.com/terraform-linters/tflint/rules"
1110
"github.com/terraform-linters/tflint/tflint"
1211
)
1312

@@ -51,7 +50,7 @@ func (cli *CLI) inspect(opts Options, dir string, filterFiles []string) int {
5150
}
5251
defer rulesetPlugin.Clean()
5352

54-
rulesets := []tflint.RuleSet{&rules.RuleSet{}}
53+
rulesets := []tflint.RuleSet{}
5554
config := cfg.ToPluginConfig()
5655
for name, ruleset := range rulesetPlugin.RuleSets {
5756
if err := ruleset.ApplyGlobalConfig(config); err != nil {
@@ -86,16 +85,6 @@ func (cli *CLI) inspect(opts Options, dir string, filterFiles []string) int {
8685
}
8786

8887
// Run inspection
89-
for _, rule := range rules.NewRules(cfg) {
90-
for _, runner := range runners {
91-
err := rule.Check(runner)
92-
if err != nil {
93-
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to check `%s` rule; %w", rule.Name(), err), cli.loader.Sources())
94-
return ExitCodeError
95-
}
96-
}
97-
}
98-
9988
for _, ruleset := range rulesetPlugin.RuleSets {
10089
for _, runner := range runners {
10190
err = ruleset.Check(plugin.NewGRPCServer(runner, rootRunner, cli.loader.Files()))

cmd/option.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,24 @@ import (
99

1010
// Options is an option specified by arguments.
1111
type Options struct {
12-
Version bool `short:"v" long:"version" description:"Print TFLint version"`
13-
Init bool `long:"init" description:"Install plugins"`
14-
Langserver bool `long:"langserver" description:"Start language server"`
15-
Format string `short:"f" long:"format" description:"Output format" choice:"default" choice:"json" choice:"checkstyle" choice:"junit" choice:"compact" choice:"sarif"`
16-
Config string `short:"c" long:"config" description:"Config file name" value-name:"FILE" default:".tflint.hcl"`
17-
IgnoreModules []string `long:"ignore-module" description:"Ignore module sources" value-name:"SOURCE"`
18-
EnableRules []string `long:"enable-rule" description:"Enable rules from the command line" value-name:"RULE_NAME"`
19-
DisableRules []string `long:"disable-rule" description:"Disable rules from the command line" value-name:"RULE_NAME"`
20-
Only []string `long:"only" description:"Enable only this rule, disabling all other defaults. Can be specified multiple times" value-name:"RULE_NAME"`
21-
EnablePlugins []string `long:"enable-plugin" description:"Enable plugins from the command line" value-name:"PLUGIN_NAME"`
22-
Varfiles []string `long:"var-file" description:"Terraform variable file name" value-name:"FILE"`
23-
Variables []string `long:"var" description:"Set a Terraform variable" value-name:"'foo=bar'"`
24-
Module bool `long:"module" description:"Inspect modules"`
25-
Force bool `long:"force" description:"Return zero exit status even if issues found"`
26-
Color bool `long:"color" description:"Enable colorized output"`
27-
NoColor bool `long:"no-color" description:"Disable colorized output"`
28-
LogLevel string `long:"loglevel" description:"Change the loglevel" choice:"trace" choice:"debug" choice:"info" choice:"warn" choice:"error"`
12+
Version bool `short:"v" long:"version" description:"Print TFLint version"`
13+
Init bool `long:"init" description:"Install plugins"`
14+
Langserver bool `long:"langserver" description:"Start language server"`
15+
Format string `short:"f" long:"format" description:"Output format" choice:"default" choice:"json" choice:"checkstyle" choice:"junit" choice:"compact" choice:"sarif"`
16+
Config string `short:"c" long:"config" description:"Config file name" value-name:"FILE" default:".tflint.hcl"`
17+
IgnoreModules []string `long:"ignore-module" description:"Ignore module sources" value-name:"SOURCE"`
18+
EnableRules []string `long:"enable-rule" description:"Enable rules from the command line" value-name:"RULE_NAME"`
19+
DisableRules []string `long:"disable-rule" description:"Disable rules from the command line" value-name:"RULE_NAME"`
20+
Only []string `long:"only" description:"Enable only this rule, disabling all other defaults. Can be specified multiple times" value-name:"RULE_NAME"`
21+
EnablePlugins []string `long:"enable-plugin" description:"Enable plugins from the command line" value-name:"PLUGIN_NAME"`
22+
Varfiles []string `long:"var-file" description:"Terraform variable file name" value-name:"FILE"`
23+
Variables []string `long:"var" description:"Set a Terraform variable" value-name:"'foo=bar'"`
24+
Module bool `long:"module" description:"Inspect modules"`
25+
Force bool `long:"force" description:"Return zero exit status even if issues found"`
26+
Color bool `long:"color" description:"Enable colorized output"`
27+
NoColor bool `long:"no-color" description:"Disable colorized output"`
28+
LogLevel string `long:"loglevel" description:"Change the loglevel" choice:"trace" choice:"debug" choice:"info" choice:"warn" choice:"error"`
29+
ActAsBundledPlugin bool `long:"act-as-bundled-plugin" hidden:"true"`
2930
}
3031

3132
func (opts *Options) toConfig() *tflint.Config {

docs/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# TFLint Documentation
22

33
- [User Guide](user-guide)
4-
- [Rules](rules)
54
- [Developer Guide](developer-guide)

docs/rules/README.md

Lines changed: 0 additions & 24 deletions
This file was deleted.

docs/rules/terraform_comment_syntax.md

Lines changed: 0 additions & 32 deletions
This file was deleted.

docs/rules/terraform_deprecated_index.md

Lines changed: 0 additions & 32 deletions
This file was deleted.

docs/rules/terraform_deprecated_interpolation.md

Lines changed: 0 additions & 38 deletions
This file was deleted.

docs/rules/terraform_documented_outputs.md

Lines changed: 0 additions & 49 deletions
This file was deleted.

docs/rules/terraform_documented_variables.md

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)