Skip to content

Commit

Permalink
Inspect local module calls by default
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 committed Nov 26, 2023
1 parent b8350bf commit 95cf7cf
Show file tree
Hide file tree
Showing 23 changed files with 732 additions and 140 deletions.
2 changes: 1 addition & 1 deletion cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ By setting TFLINT_LOG=trace, you can confirm the changes made by the autofix and
}

func (cli *CLI) setupRunners(opts Options, dir string) ([]*tflint.Runner, error) {
configs, diags := cli.loader.LoadConfig(dir, cli.config.Module)
configs, diags := cli.loader.LoadConfig(dir, cli.config.CallModuleType)
if diags.HasErrors() {
return []*tflint.Runner{}, fmt.Errorf("Failed to load configurations; %w", diags)
}
Expand Down
33 changes: 23 additions & 10 deletions cmd/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"log"
"strings"

"github.com/terraform-linters/tflint/terraform"
"github.com/terraform-linters/tflint/tflint"
)

Expand All @@ -21,8 +22,9 @@ type Options struct {
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:"Enable module inspection"`
NoModule *bool `long:"no-module" description:"Disable module inspection"`
Module *bool `long:"module" description:"Enable module inspection" hidden:"true"`
NoModule *bool `long:"no-module" description:"Disable module inspection" hidden:"true"`
CallModuleType *string `long:"call-module-type" description:"Types of module to call (default: local)" choice:"all" choice:"local" choice:"none"`
Chdir string `long:"chdir" description:"Switch to a different working directory before executing the command" value-name:"DIR"`
Recursive bool `long:"recursive" description:"Run command in each directory recursively"`
Filter []string `long:"filter" description:"Filter issues by file names or globs" value-name:"FILE"`
Expand Down Expand Up @@ -52,14 +54,25 @@ func (opts *Options) toConfig() *tflint.Config {
opts.Variables = []string{}
}

var module, moduleSet bool
callModuleType := terraform.CallLocalModule
callModuleTypeSet := false
// --call-module-type takes precedence over --module/--no-module. This is for backward compatibility.
if opts.Module != nil {
module = *opts.Module
moduleSet = true
callModuleType = terraform.CallAllModule
callModuleTypeSet = true
}
if opts.NoModule != nil {
module = !*opts.NoModule
moduleSet = true
callModuleType = terraform.CallNoModule
callModuleTypeSet = true
}
if opts.CallModuleType != nil {
var err error
callModuleType, err = terraform.AsCallModuleType(*opts.CallModuleType)
if err != nil {
// This should never happen because the option is already validated by go-flags
panic(err)
}
callModuleTypeSet = true
}

var force, forceSet bool
Expand All @@ -69,7 +82,7 @@ func (opts *Options) toConfig() *tflint.Config {
}

log.Printf("[DEBUG] CLI Options")
log.Printf("[DEBUG] Module: %t", module)
log.Printf("[DEBUG] CallModuleType: %s", callModuleType)
log.Printf("[DEBUG] Force: %t", force)
log.Printf("[DEBUG] Format: %s", opts.Format)
log.Printf("[DEBUG] Varfiles: %s", strings.Join(opts.Varfiles, ", "))
Expand Down Expand Up @@ -113,8 +126,8 @@ func (opts *Options) toConfig() *tflint.Config {
}

return &tflint.Config{
Module: module,
ModuleSet: moduleSet,
CallModuleType: callModuleType,
CallModuleTypeSet: callModuleTypeSet,

Force: force,
ForceSet: forceSet,
Expand Down
61 changes: 46 additions & 15 deletions cmd/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
flags "github.com/jessevdk/go-flags"
"github.com/terraform-linters/tflint/terraform"
"github.com/terraform-linters/tflint/tflint"
)

Expand All @@ -21,12 +22,27 @@ func Test_toConfig(t *testing.T) {
Command: "./tflint",
Expected: tflint.EmptyConfig(),
},
{
Name: "--call-module-type",
Command: "./tflint --call-module-type all",
Expected: &tflint.Config{
CallModuleType: terraform.CallAllModule,
CallModuleTypeSet: true,
Force: false,
IgnoreModules: map[string]bool{},
Varfiles: []string{},
Variables: []string{},
DisabledByDefault: false,
Rules: map[string]*tflint.RuleConfig{},
Plugins: map[string]*tflint.PluginConfig{},
},
},
{
Name: "--module",
Command: "./tflint --module",
Expected: &tflint.Config{
Module: true,
ModuleSet: true,
CallModuleType: terraform.CallAllModule,
CallModuleTypeSet: true,
Force: false,
IgnoreModules: map[string]bool{},
Varfiles: []string{},
Expand All @@ -40,8 +56,23 @@ func Test_toConfig(t *testing.T) {
Name: "--no-module",
Command: "./tflint --no-module",
Expected: &tflint.Config{
Module: false,
ModuleSet: true,
CallModuleType: terraform.CallNoModule,
CallModuleTypeSet: true,
Force: false,
IgnoreModules: map[string]bool{},
Varfiles: []string{},
Variables: []string{},
DisabledByDefault: false,
Rules: map[string]*tflint.RuleConfig{},
Plugins: map[string]*tflint.PluginConfig{},
},
},
{
Name: "--module and --call-module-type",
Command: "./tflint --module --call-module-type none",
Expected: &tflint.Config{
CallModuleType: terraform.CallNoModule,
CallModuleTypeSet: true,
Force: false,
IgnoreModules: map[string]bool{},
Varfiles: []string{},
Expand All @@ -55,7 +86,7 @@ func Test_toConfig(t *testing.T) {
Name: "--force",
Command: "./tflint --force",
Expected: &tflint.Config{
Module: false,
CallModuleType: terraform.CallLocalModule,
Force: true,
ForceSet: true,
IgnoreModules: map[string]bool{},
Expand All @@ -70,7 +101,7 @@ func Test_toConfig(t *testing.T) {
Name: "--ignore-module",
Command: "./tflint --ignore-module module1,module2",
Expected: &tflint.Config{
Module: false,
CallModuleType: terraform.CallLocalModule,
Force: false,
IgnoreModules: map[string]bool{"module1": true, "module2": true},
Varfiles: []string{},
Expand All @@ -84,7 +115,7 @@ func Test_toConfig(t *testing.T) {
Name: "multiple `--ignore-module`",
Command: "./tflint --ignore-module module1 --ignore-module module2",
Expected: &tflint.Config{
Module: false,
CallModuleType: terraform.CallLocalModule,
Force: false,
IgnoreModules: map[string]bool{"module1": true, "module2": true},
Varfiles: []string{},
Expand All @@ -98,7 +129,7 @@ func Test_toConfig(t *testing.T) {
Name: "--var-file",
Command: "./tflint --var-file example1.tfvars,example2.tfvars",
Expected: &tflint.Config{
Module: false,
CallModuleType: terraform.CallLocalModule,
Force: false,
IgnoreModules: map[string]bool{},
Varfiles: []string{"example1.tfvars", "example2.tfvars"},
Expand All @@ -112,7 +143,7 @@ func Test_toConfig(t *testing.T) {
Name: "multiple `--var-file`",
Command: "./tflint --var-file example1.tfvars --var-file example2.tfvars",
Expected: &tflint.Config{
Module: false,
CallModuleType: terraform.CallLocalModule,
Force: false,
IgnoreModules: map[string]bool{},
Varfiles: []string{"example1.tfvars", "example2.tfvars"},
Expand All @@ -126,7 +157,7 @@ func Test_toConfig(t *testing.T) {
Name: "--var",
Command: "./tflint --var foo=bar --var bar=baz",
Expected: &tflint.Config{
Module: false,
CallModuleType: terraform.CallLocalModule,
Force: false,
IgnoreModules: map[string]bool{},
Varfiles: []string{},
Expand All @@ -140,7 +171,7 @@ func Test_toConfig(t *testing.T) {
Name: "--enable-rule",
Command: "./tflint --enable-rule aws_instance_invalid_type --enable-rule aws_instance_previous_type",
Expected: &tflint.Config{
Module: false,
CallModuleType: terraform.CallLocalModule,
Force: false,
IgnoreModules: map[string]bool{},
Varfiles: []string{},
Expand All @@ -165,7 +196,7 @@ func Test_toConfig(t *testing.T) {
Name: "--disable-rule",
Command: "./tflint --disable-rule aws_instance_invalid_type --disable-rule aws_instance_previous_type",
Expected: &tflint.Config{
Module: false,
CallModuleType: terraform.CallLocalModule,
Force: false,
IgnoreModules: map[string]bool{},
Varfiles: []string{},
Expand All @@ -190,7 +221,7 @@ func Test_toConfig(t *testing.T) {
Name: "--only",
Command: "./tflint --only aws_instance_invalid_type",
Expected: &tflint.Config{
Module: false,
CallModuleType: terraform.CallLocalModule,
Force: false,
IgnoreModules: map[string]bool{},
Varfiles: []string{},
Expand All @@ -212,7 +243,7 @@ func Test_toConfig(t *testing.T) {
Name: "--enable-plugin",
Command: "./tflint --enable-plugin test --enable-plugin another-test",
Expected: &tflint.Config{
Module: false,
CallModuleType: terraform.CallLocalModule,
Force: false,
IgnoreModules: map[string]bool{},
Varfiles: []string{},
Expand All @@ -237,7 +268,7 @@ func Test_toConfig(t *testing.T) {
Name: "--format",
Command: "./tflint --format compact",
Expected: &tflint.Config{
Module: false,
CallModuleType: terraform.CallLocalModule,
Force: false,
IgnoreModules: map[string]bool{},
Varfiles: []string{},
Expand Down
2 changes: 1 addition & 1 deletion integrationtest/inspection/without_module_init/module.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ variable "instance_type" {

// terraform init did not run
module "instances" {
source = "./module"
source = "example/instances"

unknown = var.unknown
instance_type = var.instance_type
Expand Down
2 changes: 1 addition & 1 deletion langserver/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (h *handler) inspect() (map[string][]lsp.Diagnostic, error) {
return ret, fmt.Errorf("Failed to prepare loading: %w", err)
}

configs, diags := loader.LoadConfig(".", h.config.Module)
configs, diags := loader.LoadConfig(".", h.config.CallModuleType)
if diags.HasErrors() {
return ret, fmt.Errorf("Failed to load configurations: %w", diags)
}
Expand Down
Loading

0 comments on commit 95cf7cf

Please sign in to comment.