Skip to content

Commit

Permalink
Allow --chdir and --recursive to be used together (#2079)
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 committed Jul 6, 2024
1 parent a7179f0 commit 39efd18
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 10 deletions.
15 changes: 5 additions & 10 deletions cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,14 @@ func unknownOptionHandler(option string, arg flags.SplitArgument, args []string)
}

func findWorkingDirs(opts Options) ([]string, error) {
if opts.Recursive && opts.Chdir != "" {
return []string{}, errors.New("cannot use --recursive and --chdir at the same time")
baseDir := opts.Chdir
if baseDir == "" {
baseDir = "."
}

workingDirs := []string{}

if opts.Recursive {
// NOTE: The target directory is always the current directory in recursive mode
err := filepath.WalkDir(".", func(path string, d os.DirEntry, err error) error {
err := filepath.WalkDir(baseDir, func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}
Expand All @@ -171,11 +170,7 @@ func findWorkingDirs(opts Options) ([]string, error) {
return []string{}, err
}
} else {
if opts.Chdir == "" {
workingDirs = []string{"."}
} else {
workingDirs = []string{opts.Chdir}
}
workingDirs = []string{baseDir}
}

return workingDirs, nil
Expand Down
45 changes: 45 additions & 0 deletions integrationtest/recursive/chdir/result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"issues": [
{
"rule": {
"name": "aws_instance_example_type",
"severity": "error",
"link": ""
},
"message": "instance type is t2.micro",
"range": {
"filename": "subdir1/main.tf",
"start": {
"line": 2,
"column": 19
},
"end": {
"line": 2,
"column": 29
}
},
"callers": []
},
{
"rule": {
"name": "aws_instance_example_type",
"severity": "error",
"link": ""
},
"message": "instance type is t2.micro",
"range": {
"filename": "subdir1/subdir3/main.tf",
"start": {
"line": 2,
"column": 19
},
"end": {
"line": 2,
"column": 29
}
},
"callers": []
}
],
"errors": []
}
45 changes: 45 additions & 0 deletions integrationtest/recursive/chdir/result_windows.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"issues": [
{
"rule": {
"name": "aws_instance_example_type",
"severity": "error",
"link": ""
},
"message": "instance type is t2.micro",
"range": {
"filename": "subdir1\\main.tf",
"start": {
"line": 2,
"column": 19
},
"end": {
"line": 2,
"column": 29
}
},
"callers": []
},
{
"rule": {
"name": "aws_instance_example_type",
"severity": "error",
"link": ""
},
"message": "instance type is t2.micro",
"range": {
"filename": "subdir1\\subdir3\\main.tf",
"start": {
"line": 2,
"column": 19
},
"end": {
"line": 2,
"column": 29
}
},
"callers": []
}
],
"errors": []
}
7 changes: 7 additions & 0 deletions integrationtest/recursive/chdir/subdir1/.tflint.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
plugin "terraform" {
enabled = false
}

plugin "testing" {
enabled = true
}
3 changes: 3 additions & 0 deletions integrationtest/recursive/chdir/subdir1/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resource "aws_instance" "foo" {
instance_type = "t2.micro"
}
7 changes: 7 additions & 0 deletions integrationtest/recursive/chdir/subdir1/subdir3/.tflint.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
plugin "terraform" {
enabled = false
}

plugin "testing" {
enabled = true
}
3 changes: 3 additions & 0 deletions integrationtest/recursive/chdir/subdir1/subdir3/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resource "aws_instance" "foo" {
instance_type = "t2.micro"
}
7 changes: 7 additions & 0 deletions integrationtest/recursive/chdir/subdir2/.tflint.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
plugin "terraform" {
enabled = false
}

plugin "testing" {
enabled = true
}
3 changes: 3 additions & 0 deletions integrationtest/recursive/chdir/subdir2/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resource "aws_instance" "foo" {
instance_type = "t2.micro"
}
5 changes: 5 additions & 0 deletions integrationtest/recursive/recursive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ func TestIntegration(t *testing.T) {
error: true,
ignoreOrder: true,
},
{
name: "recursive + chdir",
command: "tflint --chdir=subdir1 --recursive --format json --force",
dir: "chdir",
},
}

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

0 comments on commit 39efd18

Please sign in to comment.