Skip to content

Commit

Permalink
Add support to expand glob patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastien-rosset committed Jun 28, 2022
1 parent 4cc10d8 commit c32f4fe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
20 changes: 17 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"time"
Expand Down Expand Up @@ -121,7 +122,11 @@ func rootRunE(cmd *cobra.Command, args []string) error {
return err
}

findings := p.ParsePaths(print, parseArgs(args)...)
files, err := parseArgs(args)
if err != nil {
return err
}
findings := p.ParsePaths(print, files...)

if exitOneOnFailure && findings > 0 {
// We intentionally return an error if exitOneOnFailure is true, but don't want to show usage
Expand Down Expand Up @@ -162,16 +167,25 @@ func GetRootCmd() cobra.Command {
return *rootCmd
}

func parseArgs(args []string) []string {
func parseArgs(args []string) ([]string, error) {
if len(args) == 0 {
args = parser.DefaultPath
}

if stdin {
args = []string{os.Stdin.Name()}
}
// Perform glob expansion.
var files []string
for _, arg := range args {
f, err := filepath.Glob(arg)
if err != nil {
return nil, err
}
files = append(files, f...)
}

return args
return files, nil
}

func setDebugLogLevel() {
Expand Down
16 changes: 12 additions & 4 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,20 @@ func TestParseArgs(t *testing.T) {
t.Cleanup(func() {
stdin = false
})
assert.Equal(t, parser.DefaultPath, parseArgs([]string{}))
assert.Equal(t, []string{"../.."}, parseArgs([]string{"../.."}))
files, err := parseArgs([]string{})
assert.Nil(t, err)
assert.Equal(t, parser.DefaultPath, files)
files, err = parseArgs([]string{"../.."})
assert.Nil(t, err)
assert.Equal(t, []string{"../.."}, files)

stdin = true
assert.Equal(t, []string{os.Stdin.Name()}, parseArgs([]string{}))
assert.Equal(t, []string{os.Stdin.Name()}, parseArgs([]string{"../.."}))
files, err = parseArgs([]string{})
assert.Nil(t, err)
assert.Equal(t, []string{os.Stdin.Name()}, files)
files, err = parseArgs([]string{"../.."})
assert.Nil(t, err)
assert.Equal(t, []string{os.Stdin.Name()}, files)
}

func TestRunE(t *testing.T) {
Expand Down

0 comments on commit c32f4fe

Please sign in to comment.