Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load config file in the changed directory
Browse files Browse the repository at this point in the history
wata727 committed Dec 10, 2022
1 parent 407bd21 commit a2b6654
Showing 5 changed files with 45 additions and 57 deletions.
84 changes: 42 additions & 42 deletions cmd/inspect.go
Original file line number Diff line number Diff line change
@@ -16,6 +16,25 @@ import (
)

func (cli *CLI) inspect(opts Options, dir string, filterFiles []string) int {
// Switch to a different working directory
originalWd, err := os.Getwd()
if err != nil {
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to determine current working directory; %w", err), map[string][]byte{})
return ExitCodeError
}
if opts.Chdir != "" {
if dir != "." {
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Cannot use --chdir and directory argument at the same time"), map[string][]byte{})
return ExitCodeError
}

err := os.Chdir(opts.Chdir)
if err != nil {
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to switch to a different working directory; %w", err), map[string][]byte{})
return ExitCodeError
}
}

// Setup config
cfg, err := tflint.LoadConfig(afero.Afero{Fs: afero.NewOsFs()}, opts.Config)
if err != nil {
@@ -32,10 +51,25 @@ func (cli *CLI) inspect(opts Options, dir string, filterFiles []string) int {
cfg.Merge(opts.toConfig())
cli.formatter.Format = cfg.Format

// Setup loader
cli.loader, err = terraform.NewLoader(afero.Afero{Fs: afero.NewOsFs()}, originalWd)
if err != nil {
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to prepare loading; %w", err), map[string][]byte{})
return ExitCodeError
}

// Setup runners
runners, appErr := cli.setupRunners(opts, cfg, originalWd, dir)
if appErr != nil {
cli.formatter.Print(tflint.Issues{}, appErr, cli.loader.Sources())
return ExitCodeError
}
rootRunner := runners[len(runners)-1]

// Lookup plugins and validation
rulesetPlugin, err := plugin.Discovery(cfg)
if err != nil {
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to initialize plugins; %w", err), map[string][]byte{})
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to initialize plugins; %w", err), cli.loader.Sources())
return ExitCodeError
}
defer rulesetPlugin.Clean()
@@ -49,80 +83,46 @@ func (cli *CLI) inspect(opts Options, dir string, filterFiles []string) int {
// VersionConstraints endpoint is available in tflint-plugin-sdk v0.14+.
// Skip verification if not available.
} else {
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to get TFLint version constraints to `%s` plugin; %w", name, err), map[string][]byte{})
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to get TFLint version constraints to `%s` plugin; %w", name, err), cli.loader.Sources())
return ExitCodeError
}
}
if !constraints.Check(tflint.Version) {
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to satisfy version constraints; tflint-ruleset-%s requires %s, but TFLint version is %s", name, constraints, tflint.Version), map[string][]byte{})
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to satisfy version constraints; tflint-ruleset-%s requires %s, but TFLint version is %s", name, constraints, tflint.Version), cli.loader.Sources())
return ExitCodeError
}

if err := ruleset.ApplyGlobalConfig(config); err != nil {
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to apply global config to `%s` plugin; %w", name, err), map[string][]byte{})
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to apply global config to `%s` plugin; %w", name, err), cli.loader.Sources())
return ExitCodeError
}
configSchema, err := ruleset.ConfigSchema()
if err != nil {
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to fetch config schema from `%s` plugin; %w", name, err), map[string][]byte{})
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to fetch config schema from `%s` plugin; %w", name, err), cli.loader.Sources())
return ExitCodeError
}
content := &hclext.BodyContent{}
if plugin, exists := cfg.Plugins[name]; exists {
var diags hcl.Diagnostics
content, diags = plugin.Content(configSchema)
if diags.HasErrors() {
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to parse `%s` plugin config; %w", name, diags), map[string][]byte{})
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to parse `%s` plugin config; %w", name, diags), cli.loader.Sources())
return ExitCodeError
}
}
err = ruleset.ApplyConfig(content, cfg.Sources())
if err != nil {
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to apply config to `%s` plugin; %w", name, err), map[string][]byte{})
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to apply config to `%s` plugin; %w", name, err), cli.loader.Sources())
return ExitCodeError
}

rulesets = append(rulesets, ruleset)
}
if err := cfg.ValidateRules(rulesets...); err != nil {
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to check rule config; %w", err), map[string][]byte{})
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to check rule config; %w", err), cli.loader.Sources())
return ExitCodeError
}

// Switch to a different working directory before setting up loader
originalWd, err := os.Getwd()
if err != nil {
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to determine current working directory; %w", err), map[string][]byte{})
return ExitCodeError
}
if opts.Chdir != "" {
if dir != "." {
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Cannot use --chdir and directory argument at the same time"), map[string][]byte{})
return ExitCodeError
}

err := os.Chdir(opts.Chdir)
if err != nil {
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to switch to a different working directory; %w", err), map[string][]byte{})
return ExitCodeError
}
}

// Setup loader
cli.loader, err = terraform.NewLoader(afero.Afero{Fs: afero.NewOsFs()}, originalWd)
if err != nil {
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to prepare loading; %w", err), map[string][]byte{})
return ExitCodeError
}

// Setup runners
runners, appErr := cli.setupRunners(opts, cfg, originalWd, dir)
if appErr != nil {
cli.formatter.Print(tflint.Issues{}, appErr, cli.loader.Sources())
return ExitCodeError
}
rootRunner := runners[len(runners)-1]

// Run inspection
for _, ruleset := range rulesetPlugin.RuleSets {
for _, runner := range runners {
1 change: 0 additions & 1 deletion docs/user-guide/README.md
Original file line number Diff line number Diff line change
@@ -10,5 +10,4 @@ This guide describes the various features of TFLint for end users.
- [Module Inspection](module-inspection.md)
- [Annotations](annotations.md)
- [Compatibility with Terraform](compatibility.md)
- [Switching working directory with --chdir](working_directory.md)
- [Editor Integration](editor-integration.md)
14 changes: 0 additions & 14 deletions docs/user-guide/working_directory.md

This file was deleted.

3 changes: 3 additions & 0 deletions integrationtest/cli/multiple_files/subdir/.tflint.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugin "testing" {
enabled = true
}

0 comments on commit a2b6654

Please sign in to comment.