diff --git a/Makefile b/Makefile index 09cf89b29..6bfeb8414 100644 --- a/Makefile +++ b/Makefile @@ -13,8 +13,8 @@ build: install: go install -e2e: prepare install - go test -timeout 5m ./integrationtest/inspection ./integrationtest/langserver ./integrationtest/bundled ./integrationtest/init +e2e: prepare + go test -timeout 5m ./integrationtest/inspection ./integrationtest/langserver ./integrationtest/init lint: golangci-lint run ./... diff --git a/cmd/cli.go b/cmd/cli.go index 70ddcc85c..da17fc8d0 100644 --- a/cmd/cli.go +++ b/cmd/cli.go @@ -93,8 +93,6 @@ func (cli *CLI) Run(args []string) int { return cli.init(opts) case opts.Langserver: return cli.startLanguageServer(opts.Config, opts.toConfig()) - case opts.ActAsAwsPlugin: - return cli.actAsAwsPlugin() default: return cli.inspect(opts, dir, filterFiles) } diff --git a/cmd/inspect.go b/cmd/inspect.go index ada283eee..72db7ad0d 100644 --- a/cmd/inspect.go +++ b/cmd/inspect.go @@ -2,7 +2,6 @@ package cmd import ( "fmt" - "log" "github.com/spf13/afero" tfplugin "github.com/terraform-linters/tflint/plugin" @@ -41,25 +40,6 @@ func (cli *CLI) inspect(opts Options, dir string, filterFiles []string) int { } rootRunner := runners[len(runners)-1] - // AWS plugin is automatically enabled from your provider requirements, even if the plugin isn't explicitly enabled. - if _, exists := cfg.Plugins["aws"]; !exists { - reqs, diags := rootRunner.TFConfig.ProviderRequirements() - if diags.HasErrors() { - cli.formatter.Print(tflint.Issues{}, tflint.NewContextError("Failed to get Terraform provider requirements", diags), cli.loader.Sources()) - return ExitCodeError - } - for addr := range reqs { - if addr.Type == "aws" { - log.Print("[INFO] AWS provider requirements found. Enable the plugin `aws` automatically") - fmt.Fprintln(cli.errStream, "WARNING: The plugin `aws` is not explicitly enabled. The bundled plugin will be enabled instead, but it is deprecated and will be removed in a future version. Please see https://github.com/terraform-linters/tflint/pull/1160 for details.") - cfg.Plugins["aws"] = &tflint.PluginConfig{ - Name: "aws", - Enabled: true, - } - } - } - } - // Lookup plugins and validation plugin, err := tfplugin.Discovery(cfg) if err != nil { diff --git a/cmd/internal_aws_plugin.go b/cmd/internal_aws_plugin.go deleted file mode 100644 index b79686181..000000000 --- a/cmd/internal_aws_plugin.go +++ /dev/null @@ -1,27 +0,0 @@ -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-aws/aws" - "github.com/terraform-linters/tflint-ruleset-aws/project" - "github.com/terraform-linters/tflint-ruleset-aws/rules" - "github.com/terraform-linters/tflint-ruleset-aws/rules/api" -) - -func (cli *CLI) actAsAwsPlugin() int { - plugin.Serve(&plugin.ServeOpts{ - RuleSet: &aws.RuleSet{ - BuiltinRuleSet: tflint.BuiltinRuleSet{ - Name: "aws", - Version: fmt.Sprintf("%s-bundled", project.Version), - Rules: rules.Rules, - }, - APIRules: api.Rules, - }, - }) - - return ExitCodeOK -} diff --git a/cmd/option.go b/cmd/option.go index a7dbfa778..9fc63d712 100644 --- a/cmd/option.go +++ b/cmd/option.go @@ -10,23 +10,22 @@ 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" default:"default"` - 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"` - 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"` - ActAsAwsPlugin bool `long:"act-as-aws-plugin" hidden:"true"` + 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" default:"default"` + 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"` + 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"` } func (opts *Options) toConfig() *tflint.Config { diff --git a/cmd/version.go b/cmd/version.go index 0bf5e3081..67ca90c2c 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -4,7 +4,6 @@ import ( "fmt" "log" - "github.com/spf13/afero" tfplugin "github.com/terraform-linters/tflint/plugin" "github.com/terraform-linters/tflint/tflint" ) @@ -25,38 +24,6 @@ func (cli *CLI) printVersion(opts Options) int { } cfg = cfg.Merge(opts.toConfig()) - cli.loader, err = tflint.NewLoader(afero.Afero{Fs: afero.NewOsFs()}, cfg) - if err != nil { - log.Printf("[ERROR] Failed to prepare loading: %s", err) - return ExitCodeOK - } - - runners, appErr := cli.setupRunners(opts, cfg, ".") - if appErr != nil { - log.Printf("[ERROR] Failed to setup runners: %s", appErr) - return ExitCodeOK - } - rootRunner := runners[len(runners)-1] - - // AWS plugin is automatically enabled from your provider requirements, even if the plugin isn't explicitly enabled. - if _, exists := cfg.Plugins["aws"]; !exists { - reqs, diags := rootRunner.TFConfig.ProviderRequirements() - if diags.HasErrors() { - log.Printf("[ERROR] Failed to get Terraform provider requirements: %s", diags) - return ExitCodeOK - } - for addr := range reqs { - if addr.Type == "aws" { - log.Print("[INFO] AWS provider requirements found. Enable the plugin `aws` automatically") - fmt.Fprintln(cli.errStream, "WARNING: The plugin `aws` is not explicitly enabled. The bundled plugin will be enabled instead, but it is deprecated and will be removed in a future version. Please see https://github.com/terraform-linters/tflint/pull/1160 for details.") - cfg.Plugins["aws"] = &tflint.PluginConfig{ - Name: "aws", - Enabled: true, - } - } - } - } - plugin, err := tfplugin.Discovery(cfg) if err != nil { log.Printf("[ERROR] Failed to initialize plugins: %s", err) diff --git a/integrationtest/bundled/basic/template.tf b/integrationtest/bundled/basic/template.tf deleted file mode 100644 index 9218d1984..000000000 --- a/integrationtest/bundled/basic/template.tf +++ /dev/null @@ -1,3 +0,0 @@ -resource "aws_instance" "foo" { - instance_type = "t1.2xlarge" -} diff --git a/integrationtest/bundled/bundled_test.go b/integrationtest/bundled/bundled_test.go deleted file mode 100644 index 9c86df1eb..000000000 --- a/integrationtest/bundled/bundled_test.go +++ /dev/null @@ -1,150 +0,0 @@ -package main - -import ( - "bytes" - "encoding/json" - "fmt" - "io" - "log" - "os" - "os/exec" - "path/filepath" - "runtime" - "strings" - "testing" - - "github.com/google/go-cmp/cmp" - "github.com/google/go-cmp/cmp/cmpopts" - "github.com/terraform-linters/tflint/formatter" -) - -func TestMain(m *testing.M) { - log.SetOutput(io.Discard) - os.Exit(m.Run()) -} - -func TestBundledPlugin(t *testing.T) { - cases := []struct { - Name string - Command string - Dir string - }{ - { - Name: "basic", - Command: "tflint --format json --force", - Dir: "basic", - }, - { - // Regression: https://github.com/terraform-linters/tflint-ruleset-aws/issues/41 - Name: "rule config", - Command: "tflint --format json --force", - Dir: "rule-config", - }, - { - // Regression: https://github.com/terraform-linters/tflint/issues/1028 - Name: "deep checking rule config", - Command: "tflint --format json --force", - Dir: "deep-checking-rule-config", - }, - { - // Regression: https://github.com/terraform-linters/tflint/issues/1029 - Name: "heredoc", - Command: "tflint --format json --force", - Dir: "heredoc", - }, - { - // Regression: https://github.com/terraform-linters/tflint/issues/1054 - Name: "disabled-rules", - Command: "tflint --format json --force", - Dir: "disabled-rules", - }, - { - // Regression: https://github.com/terraform-linters/tflint-ruleset-aws/issues/48 - Name: "cty-based-eval", - Command: "tflint --format json --force", - Dir: "cty-based-eval", - }, - { - // Regression: https://github.com/terraform-linters/tflint/issues/1102 - Name: "map-attribute", - Command: "tflint --format json --force", - Dir: "map-attribute", - }, - { - // Regression: https://github.com/terraform-linters/tflint/issues/1103 - Name: "rule config with --enable-rule", - Command: "tflint --enable-rule aws_s3_bucket_name --format json --force", - Dir: "rule-config", - }, - { - Name: "rule config with --only", - Command: "tflint --only aws_s3_bucket_name --format json --force", - Dir: "rule-config", - }, - } - - dir, _ := os.Getwd() - for _, tc := range cases { - t.Run(tc.Name, func(t *testing.T) { - testDir := filepath.Join(dir, tc.Dir) - - t.Cleanup(func() { - if err := os.Chdir(dir); err != nil { - t.Fatal(err) - } - }) - - if err := os.Chdir(testDir); err != nil { - t.Fatal(err) - } - - args := strings.Split(tc.Command, " ") - var cmd *exec.Cmd - if runtime.GOOS == "windows" { - cmd = exec.Command("tflint.exe", args[1:]...) - } else { - cmd = exec.Command("tflint", args[1:]...) - } - outStream, errStream := new(bytes.Buffer), new(bytes.Buffer) - cmd.Stdout = outStream - cmd.Stderr = errStream - - if err := cmd.Run(); err != nil { - fmt.Fprintf(os.Stderr, "Failed to exec command (%s)\n", err) - } - - var b []byte - var err error - if runtime.GOOS == "windows" && IsWindowsResultExist() { - b, err = os.ReadFile(filepath.Join(testDir, "result_windows.json")) - } else { - b, err = os.ReadFile(filepath.Join(testDir, "result.json")) - } - if err != nil { - t.Fatal(err) - } - - var expected *formatter.JSONOutput - if err := json.Unmarshal(b, &expected); err != nil { - t.Fatal(err) - } - - var got *formatter.JSONOutput - if err := json.Unmarshal(outStream.Bytes(), &got); err != nil { - t.Fatal(err) - } - - opts := []cmp.Option{ - cmpopts.IgnoreFields(formatter.JSONRule{}, "Link"), - } - if !cmp.Equal(got, expected, opts...) { - t.Fatalf("diff=%s", cmp.Diff(expected, got)) - } - }) - } -} - -func IsWindowsResultExist() bool { - _, err := os.Stat("result_windows.json") - return !os.IsNotExist(err) -} diff --git a/integrationtest/bundled/cty-based-eval/.tflint.hcl b/integrationtest/bundled/cty-based-eval/.tflint.hcl deleted file mode 100644 index 37107e1a1..000000000 --- a/integrationtest/bundled/cty-based-eval/.tflint.hcl +++ /dev/null @@ -1,4 +0,0 @@ -rule "aws_resource_missing_tags" { - enabled = true - tags = ["Environment", "Name", "Type"] -} diff --git a/integrationtest/bundled/cty-based-eval/result.json b/integrationtest/bundled/cty-based-eval/result.json deleted file mode 100644 index a0bae126f..000000000 --- a/integrationtest/bundled/cty-based-eval/result.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "issues": [ - { - "rule": { - "name": "aws_resource_missing_tags", - "severity": "info", - "link": "https://github.com/terraform-linters/tflint-ruleset-aws/blob/v0.4.1/docs/rules/aws_resource_missing_tags.md" - }, - "message": "The resource is missing the following tags: \"Environment\", \"Name\", \"Type\".", - "range": { - "filename": "template.tf", - "start": { - "line": 5, - "column": 1 - }, - "end": { - "line": 5, - "column": 41 - } - }, - "callers": [] - } - ], - "errors": [] -} diff --git a/integrationtest/bundled/cty-based-eval/template.tf b/integrationtest/bundled/cty-based-eval/template.tf deleted file mode 100644 index fa1dde2a9..000000000 --- a/integrationtest/bundled/cty-based-eval/template.tf +++ /dev/null @@ -1,7 +0,0 @@ -variable "tags" { - default = [] -} - -resource "aws_autoscaling_group" "group" { - tags = var.tags -} diff --git a/integrationtest/bundled/deep-checking-rule-config/.tflint.hcl b/integrationtest/bundled/deep-checking-rule-config/.tflint.hcl deleted file mode 100644 index eee1cf26e..000000000 --- a/integrationtest/bundled/deep-checking-rule-config/.tflint.hcl +++ /dev/null @@ -1,7 +0,0 @@ -plugin "aws" { - enabled = true -} - -rule "aws_instance_invalid_ami" { - enabled = false -} diff --git a/integrationtest/bundled/deep-checking-rule-config/result.json b/integrationtest/bundled/deep-checking-rule-config/result.json deleted file mode 100644 index 6a49e6524..000000000 --- a/integrationtest/bundled/deep-checking-rule-config/result.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "issues": [], - "errors": [] -} diff --git a/integrationtest/bundled/disabled-rules/.tflint.hcl b/integrationtest/bundled/disabled-rules/.tflint.hcl deleted file mode 100644 index afa0cae78..000000000 --- a/integrationtest/bundled/disabled-rules/.tflint.hcl +++ /dev/null @@ -1,11 +0,0 @@ -plugin "aws" { - enabled = true -} - -rule "aws_instance_invalid_type" { - enabled = true -} - -rule "aws_instance_previous_type" { - enabled = false -} diff --git a/integrationtest/bundled/disabled-rules/template.tf b/integrationtest/bundled/disabled-rules/template.tf deleted file mode 100644 index a00a43a72..000000000 --- a/integrationtest/bundled/disabled-rules/template.tf +++ /dev/null @@ -1,7 +0,0 @@ -resource "aws_instance" "invalid" { - instance_type = "t1.2xlarge" -} - -resource "aws_instance" "previous" { - instance_type = "t1.micro" -} diff --git a/integrationtest/bundled/heredoc/template.tf b/integrationtest/bundled/heredoc/template.tf deleted file mode 100644 index d0faf14ab..000000000 --- a/integrationtest/bundled/heredoc/template.tf +++ /dev/null @@ -1,5 +0,0 @@ -resource "aws_ecr_lifecycle_policy" "test" { - policy = <