Skip to content

Commit

Permalink
Switch internal Terraform rules to the bundled plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 committed Sep 3, 2022
1 parent 46b52b6 commit 607e1e9
Show file tree
Hide file tree
Showing 84 changed files with 624 additions and 9,538 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ build:
install:
go install

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

lint:
Expand Down
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ A Pluggable [Terraform](https://www.terraform.io/) Linter

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

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

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

## Getting Started

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:
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.

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:

```hcl
plugin "terraform" {
enabled = true
preset = "recommended"
}
```

See the [tflint-ruleset-terraform documentation](https://github.com/terraform-linters/tflint-ruleset-terraform/blob/main/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:

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

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.

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`.

```hcl
Expand Down
24 changes: 24 additions & 0 deletions cmd/bundled_plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmd

import (
"fmt"

"github.com/terraform-linters/tflint-plugin-sdk/plugin"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
"github.com/terraform-linters/tflint-ruleset-terraform/project"
"github.com/terraform-linters/tflint-ruleset-terraform/rules"
"github.com/terraform-linters/tflint-ruleset-terraform/terraform"
)

func (cli *CLI) actAsBundledPlugin() int {
plugin.Serve(&plugin.ServeOpts{
RuleSet: &terraform.RuleSet{
BuiltinRuleSet: tflint.BuiltinRuleSet{
Name: "terraform",
Version: fmt.Sprintf("%s-bundled", project.Version),
},
PresetRules: rules.PresetRules,
},
})
return ExitCodeOK
}
2 changes: 2 additions & 0 deletions cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ func (cli *CLI) Run(args []string) int {
return cli.init(opts)
case opts.Langserver:
return cli.startLanguageServer(opts.Config, opts.toConfig())
case opts.ActAsBundledPlugin:
return cli.actAsBundledPlugin()
default:
return cli.inspect(opts, dir, filterFiles)
}
Expand Down
13 changes: 1 addition & 12 deletions cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/spf13/afero"
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
"github.com/terraform-linters/tflint/plugin"
"github.com/terraform-linters/tflint/rules"
"github.com/terraform-linters/tflint/tflint"
)

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

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

// Run inspection
for _, rule := range rules.NewRules(cfg) {
for _, runner := range runners {
err := rule.Check(runner)
if err != nil {
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to check `%s` rule; %w", rule.Name(), err), cli.loader.Sources())
return ExitCodeError
}
}
}

for _, ruleset := range rulesetPlugin.RuleSets {
for _, runner := range runners {
err = ruleset.Check(plugin.NewGRPCServer(runner, rootRunner, cli.loader.Files()))
Expand Down
35 changes: 18 additions & 17 deletions cmd/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,24 @@ import (

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

func (opts *Options) toConfig() *tflint.Config {
Expand Down
1 change: 0 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# TFLint Documentation

- [User Guide](user-guide)
- [Rules](rules)
- [Developer Guide](developer-guide)
24 changes: 0 additions & 24 deletions docs/rules/README.md

This file was deleted.

32 changes: 0 additions & 32 deletions docs/rules/terraform_comment_syntax.md

This file was deleted.

32 changes: 0 additions & 32 deletions docs/rules/terraform_deprecated_index.md

This file was deleted.

38 changes: 0 additions & 38 deletions docs/rules/terraform_deprecated_interpolation.md

This file was deleted.

49 changes: 0 additions & 49 deletions docs/rules/terraform_documented_outputs.md

This file was deleted.

49 changes: 0 additions & 49 deletions docs/rules/terraform_documented_variables.md

This file was deleted.

Loading

0 comments on commit 607e1e9

Please sign in to comment.