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 d62bf5c
Show file tree
Hide file tree
Showing 64 changed files with 580 additions and 8,420 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
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
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/terraform-linters/tflint
go 1.19

require (
github.com/Masterminds/semver/v3 v3.1.1
github.com/agext/levenshtein v1.2.3
github.com/apparentlymart/go-cidr v1.1.0
github.com/bmatcuk/doublestar v1.1.5
Expand All @@ -12,13 +11,11 @@ require (
github.com/google/go-cmp v0.5.8
github.com/google/go-github/v35 v35.3.0
github.com/google/uuid v1.3.0
github.com/hashicorp/go-getter v1.6.2
github.com/hashicorp/go-plugin v1.4.5
github.com/hashicorp/go-uuid v1.0.3
github.com/hashicorp/go-version v1.6.0
github.com/hashicorp/hcl/v2 v2.13.0
github.com/hashicorp/logutils v1.0.0
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c
github.com/jessevdk/go-flags v1.5.0
github.com/jstemmer/go-junit-report v1.0.0
github.com/mattn/go-colorable v0.1.13
Expand All @@ -27,7 +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.11.1-0.20220812135228-391859ca83f5
github.com/terraform-linters/tflint-plugin-sdk v0.11.1-0.20220821130728-3b6ea1aa3821
github.com/terraform-linters/tflint-ruleset-terraform v0.0.0-20220828152837-bc67b803df5b
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 All @@ -41,6 +39,7 @@ require (
require (
cloud.google.com/go v0.75.0 // indirect
cloud.google.com/go/storage v1.14.0 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/aws/aws-sdk-go v1.42.43 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
Expand All @@ -49,8 +48,10 @@ require (
github.com/google/go-querystring v1.0.0 // indirect
github.com/googleapis/gax-go/v2 v2.0.5 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-getter v1.6.2 // indirect
github.com/hashicorp/go-hclog v1.2.2 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c // indirect
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,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.11.1-0.20220812135228-391859ca83f5 h1:U90aIT3pRv9zDCf1aIWclXyRnjIILe6BvrDDywrGBMo=
github.com/terraform-linters/tflint-plugin-sdk v0.11.1-0.20220812135228-391859ca83f5/go.mod h1:g5BTK4enaWI/EPr2qWfRDJU/Qqnu84Y33JTETyVxxMA=
github.com/terraform-linters/tflint-plugin-sdk v0.11.1-0.20220821130728-3b6ea1aa3821 h1:EcgqevoO56J8zEbiEKVesICh4CaPY6+/JWbtxLnuUFw=
github.com/terraform-linters/tflint-plugin-sdk v0.11.1-0.20220821130728-3b6ea1aa3821/go.mod h1:E+gbw/AE/SDuvqh2RgVykKuQLleDd/iYP55DE1lLKac=
github.com/terraform-linters/tflint-ruleset-terraform v0.0.0-20220828152837-bc67b803df5b h1:5D4knL9Th0BZecqRuMvEJZqvZCEqZcGXRT/ZqtGW6tM=
github.com/terraform-linters/tflint-ruleset-terraform v0.0.0-20220828152837-bc67b803df5b/go.mod h1:ZM2YzKRBSFXClWekQD8OTF7XhTEjllMA6ikeq76JTuQ=
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
10 changes: 10 additions & 0 deletions integrationtest/bundled/basic/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
variable "instance_type" {}
variable "unused" {
type = string
}

resource "aws_instance" "main" {
count = [] == [] ? 1 : 0

instance_type = "${var.instance_type}"
}
125 changes: 125 additions & 0 deletions integrationtest/bundled/basic/result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
{
"issues": [
{
"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"
},
"message": "terraform \"required_version\" attribute is required",
"range": {
"filename": "",
"start": {
"line": 0,
"column": 0
},
"end": {
"line": 0,
"column": 0
}
},
"callers": []
},
{
"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"
},
"message": "`instance_type` variable has no type",
"range": {
"filename": "main.tf",
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 25
}
},
"callers": []
},
{
"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": 2,
"column": 1
},
"end": {
"line": 2,
"column": 18
}
},
"callers": []
},
{
"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"
},
"message": "Missing version constraint for provider \"aws\" in \"required_providers\"",
"range": {
"filename": "main.tf",
"start": {
"line": 6,
"column": 1
},
"end": {
"line": 6,
"column": 31
}
},
"callers": []
},
{
"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"
},
"message": "Comparing a collection with an empty list is invalid. To detect an empty collection, check its length.",
"range": {
"filename": "main.tf",
"start": {
"line": 7,
"column": 11
},
"end": {
"line": 7,
"column": 19
}
},
"callers": []
},
{
"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"
},
"message": "Interpolation-only expressions are deprecated in Terraform v0.12.14",
"range": {
"filename": "main.tf",
"start": {
"line": 9,
"column": 19
},
"end": {
"line": 9,
"column": 41
}
},
"callers": []
}
],
"errors": []
}
Loading

0 comments on commit d62bf5c

Please sign in to comment.