Skip to content

Commit

Permalink
Add --filter option (#1638)
Browse files Browse the repository at this point in the history
* Add --filter option

* Add support for glob patterns
  • Loading branch information
wata727 authored Jan 7, 2023
1 parent 8ddbb49 commit 9ab5262
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ TFLint inspects files under the current directory by default. You can change the
```
$ tflint --help
Usage:
tflint [OPTIONS] [FILE or DIR...]
tflint --chdir=DIR/--recursive [OPTIONS]
Application Options:
-v, --version Print TFLint version
Expand All @@ -141,6 +141,7 @@ Application Options:
--module Inspect modules
--chdir=DIR Switch to a different working directory before executing the command
--recursive Run command in each directory recursively
--filter=FILE Filter issues by file names or globs.
--force Return zero exit status even if issues found
--color Enable colorized output
--no-color Disable colorized output
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func NewCLI(outStream io.Writer, errStream io.Writer) (*CLI, error) {
func (cli *CLI) Run(args []string) int {
var opts Options
parser := flags.NewParser(&opts, flags.HelpFlag)
parser.Usage = "[OPTIONS] [FILE or DIR...]"
parser.Usage = "--chdir=DIR/--recursive [OPTIONS]"
parser.UnknownOptionHandler = unknownOptionHandler
// Parse commandline flag
args, err := parser.ParseArgs(args)
Expand Down
15 changes: 15 additions & 0 deletions cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ func (cli *CLI) inspect(opts Options, args []string) int {
if opts.Recursive && (targetDir != "." || len(filterFiles) > 0) {
return fmt.Errorf("Cannot use --recursive and arguments at the same time")
}
if len(opts.Filter) > 0 && (targetDir != "." || len(filterFiles) > 0) {
return fmt.Errorf("Cannot use --filter and arguments at the same time")
}

for _, pattern := range opts.Filter {
files, err := filepath.Glob(pattern)
if err != nil {
return fmt.Errorf("Failed to parse --filter options; %w", err)
}
// Add the raw pattern to return an empty result if it doesn't match any files
if len(files) == 0 {
filterFiles = append(filterFiles, pattern)
}
filterFiles = append(filterFiles, files...)
}

// Join with the working directory to create the fullpath
for i, file := range filterFiles {
Expand Down
1 change: 1 addition & 0 deletions cmd/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Options struct {
Module bool `long:"module" description:"Inspect modules"`
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"`
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"`
Expand Down
43 changes: 43 additions & 0 deletions integrationtest/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,35 @@ func TestIntegration(t *testing.T) {
status: cmd.ExitCodeError,
stderr: fmt.Sprintf("Failed to load `%s`: Multiple files in different directories are not allowed", filepath.Join("subdir", "main.tf")),
},
{
name: "--filter",
command: "./tflint --filter=empty.tf",
dir: "multiple_files",
status: cmd.ExitCodeOK,
stdout: "", // main.tf is ignored
},
{
name: "--filter with multiple files",
command: "./tflint --filter=empty.tf --filter=main.tf",
dir: "multiple_files",
status: cmd.ExitCodeIssuesFound,
// main.tf is not ignored
stdout: fmt.Sprintf("%s (aws_instance_example_type)", color.New(color.Bold).Sprint("instance type is t2.micro")),
},
{
name: "--filter with glob (files found)",
command: "./tflint --filter=*.tf",
dir: "multiple_files",
status: cmd.ExitCodeIssuesFound,
stdout: fmt.Sprintf("%s (aws_instance_example_type)", color.New(color.Bold).Sprint("instance type is t2.micro")),
},
{
name: "--filter with glob (files not found)",
command: "./tflint --filter=*_generated.tf",
dir: "multiple_files",
status: cmd.ExitCodeOK,
stdout: "",
},
{
name: "--chdir",
command: "./tflint --chdir=subdir",
Expand Down Expand Up @@ -302,6 +331,13 @@ func TestIntegration(t *testing.T) {
status: cmd.ExitCodeError,
stderr: "Cannot use --chdir and directory argument at the same time",
},
{
name: "--chdir and --filter",
command: "./tflint --chdir=subdir --filter=main.tf",
dir: "chdir",
status: cmd.ExitCodeIssuesFound,
stdout: fmt.Sprintf("%s (aws_instance_example_type)", color.New(color.Bold).Sprint("instance type is m5.2xlarge")),
},
{
name: "--recursive and file argument",
command: "./tflint --recursive main.tf",
Expand All @@ -316,6 +352,13 @@ func TestIntegration(t *testing.T) {
status: cmd.ExitCodeError,
stderr: "Cannot use --recursive and arguments at the same time",
},
{
name: "--recursive and --filter",
command: "./tflint --recursive --filter=main.tf",
dir: "chdir",
status: cmd.ExitCodeIssuesFound,
stdout: fmt.Sprintf("%s (aws_instance_example_type)", color.New(color.Bold).Sprint("instance type is m5.2xlarge")),
},
}

dir, _ := os.Getwd()
Expand Down

0 comments on commit 9ab5262

Please sign in to comment.