Skip to content

Commit 75aa36e

Browse files
wata727bendrucker
andauthored
Add --recursive option (terraform-linters#1622)
* Add --recursive option * Apply suggestions from code review Co-authored-by: Ben Drucker <[email protected]> * Use defer to return to the original working directory Co-authored-by: Ben Drucker <[email protected]>
1 parent 5f4f965 commit 75aa36e

File tree

23 files changed

+468
-111
lines changed

23 files changed

+468
-111
lines changed

README.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ Application Options:
141141
--module Inspect modules
142142
--chdir=DIR Switch to a different working directory before running inspection
143143
--force Return zero exit status even if issues found
144+
--recursive Inspect directories recursively
144145
--color Enable colorized output
145146
--no-color Disable colorized output
146147
@@ -150,16 +151,6 @@ Help Options:
150151

151152
See [User Guide](docs/user-guide) for details.
152153

153-
## FAQ
154-
155-
### Does TFLint check modules recursively?
156-
No. TFLint always checks only the current root module (no recursive check). However, you can check calling child modules based on module arguments by enabling [Module Inspection](docs/user-guide/module-inspection.md). This allows you to check that you are not passing illegal values to the module.
157-
158-
Note that if you want to recursively inspect local modules, you need to run them in each directory. This is a limitation that occurs because Terraform always works for one directory. TFLint tries to emulate Terraform's semantics, so cannot perform recursive inspection.
159-
160-
### Do I need to install Terraform for TFLint to work?
161-
No. TFLint works as a single binary because Terraform is embedded as a library. Note that this means that the version of Terraform used is determined for each TFLint version. See also [Compatibility with Terraform](docs/user-guide/compatibility.md).
162-
163154
## Debugging
164155

165156
If you don't get the expected behavior, you can see the detailed logs when running with `TFLINT_LOG` environment variable.

cmd/cli.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,25 @@ type CLI struct {
2929
// outStream and errStream are the stdout and stderr
3030
// to write message from the CLI.
3131
outStream, errStream io.Writer
32-
loader *terraform.Loader
33-
formatter *formatter.Formatter
32+
originalWorkingDir string
33+
sources map[string][]byte
34+
35+
// fields for each module
36+
config *tflint.Config
37+
loader *terraform.Loader
38+
formatter *formatter.Formatter
3439
}
3540

3641
// NewCLI returns new CLI initialized by input streams
37-
func NewCLI(outStream io.Writer, errStream io.Writer) *CLI {
42+
func NewCLI(outStream io.Writer, errStream io.Writer) (*CLI, error) {
43+
wd, err := os.Getwd()
44+
3845
return &CLI{
39-
outStream: outStream,
40-
errStream: errStream,
41-
}
46+
outStream: outStream,
47+
errStream: errStream,
48+
originalWorkingDir: wd,
49+
sources: map[string][]byte{},
50+
}, err
4251
}
4352

4453
// Run invokes the CLI with the given arguments.

cmd/init.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ func (cli *CLI) init(opts Options) int {
1515
fmt.Fprintf(cli.errStream, "Cannot use --chdir with --init\n")
1616
return ExitCodeError
1717
}
18+
if opts.Recursive {
19+
fmt.Fprintf(cli.errStream, "Cannot use --recursive with --init\n")
20+
return ExitCodeError
21+
}
1822

1923
cfg, err := tflint.LoadConfig(afero.Afero{Fs: afero.NewOsFs()}, opts.Config)
2024
if err != nil {

0 commit comments

Comments
 (0)