Skip to content

Commit

Permalink
feat(misconf): Add glob support to --skip-dirs and --skip-files o…
Browse files Browse the repository at this point in the history
…ptions

Addresses: #3754

Signed-off-by: Simar <simar@linux.com>
  • Loading branch information
simar7 committed Mar 18, 2023
1 parent 09fd299 commit b400110
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
19 changes: 19 additions & 0 deletions pkg/flag/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -249,6 +250,24 @@ func getValue(flag *Flag) any {
return viper.Get(flag.ConfigName)
}

func explodeGlob(paths []string, dir string) []string {
var exploded []string

for _, path := range paths {
if !strings.Contains(path, "*") {
exploded = append(exploded, path)
continue
}
if globPaths, err := filepath.Glob(filepath.Join(dir, path)); err == nil {
for _, globPath := range globPaths {
exploded = append(exploded, globPath)
}
}
}

return exploded
}

func (f *Flags) groups() []FlagGroup {
var groups []FlagGroup
// This order affects the usage message, so they are sorted by frequency of use.
Expand Down
32 changes: 32 additions & 0 deletions pkg/flag/options_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package flag

import (
"fmt"
"testing"

"github.com/spf13/viper"
Expand Down Expand Up @@ -80,3 +81,34 @@ func Test_getStringSlice(t *testing.T) {
})
}
}

func Test_explodeGlob(t *testing.T) {
testCases := []struct {
skipInput []string
want []string
}{
{
skipInput: []string{"./testdata/*/*"},
want: []string{"testdata/.dotdir/bar", "testdata/.dotdir/foo"},
},
{
skipInput: []string{"./testdata/*/bar"},
want: []string{"testdata/.dotdir/bar"},
},
{
skipInput: []string{"path/with/no/glob"},
want: []string{"path/with/no/glob"},
},
{
skipInput: []string{"./testdata/doesnotexist/*"},
want: []string(nil),
},
}

for i, tc := range testCases {
t.Run(fmt.Sprint(i), func(t *testing.T) {
assert.Equal(t, tc.want, explodeGlob(tc.skipInput, "."))
})
}

}
4 changes: 2 additions & 2 deletions pkg/flag/scan_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ func (f *ScanFlagGroup) ToOptions(args []string) (ScanOptions, error) {

return ScanOptions{
Target: target,
SkipDirs: getStringSlice(f.SkipDirs),
SkipFiles: getStringSlice(f.SkipFiles),
SkipDirs: explodeGlob(getStringSlice(f.SkipDirs), target),
SkipFiles: explodeGlob(getStringSlice(f.SkipFiles), target),
OfflineScan: getBool(f.OfflineScan),
Scanners: scanners,
FilePatterns: getStringSlice(f.FilePatterns),
Expand Down
1 change: 1 addition & 0 deletions pkg/flag/testdata/.dotdir/bar
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bar
1 change: 1 addition & 0 deletions pkg/flag/testdata/.dotdir/foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo

0 comments on commit b400110

Please sign in to comment.