diff --git a/filelist.go b/filelist.go index ac767ed..b0b399b 100644 --- a/filelist.go +++ b/filelist.go @@ -15,10 +15,9 @@ package main import ( + "regexp" "sort" "strings" - - "github.com/ryanuber/go-glob" ) // fileList uses a map for patterns to ensure each pattern only @@ -31,7 +30,9 @@ func newFileList(paths ...string) *fileList { f := &fileList{ patterns: make(map[string]struct{}), } + var replacer = strings.NewReplacer("*", ".*") for _, p := range paths { + p = replacer.Replace(p) f.patterns[p] = struct{}{} } return f @@ -57,31 +58,27 @@ func (f *fileList) Set(path string) error { func (f fileList) Contains(path string) bool { for p := range f.patterns { - if strings.Contains(p, glob.GLOB) { - if glob.Glob(p, path) { - if logger != nil { - logger.Printf("skipping: %s\n", path) - } - return true + // check if the path matches the regular expression pattern + r, err := regexp.Compile(p) + if err != nil { + if logger != nil { + logger.Printf("invalid pattern: %s\n", p) } - } else { - // check if only a sub-folder of the path is excluded - if strings.Contains(path, p) { - if logger != nil { - logger.Printf("skipping: %s\n", path) - } - return true + continue + } + if r.MatchString(path) { + if logger != nil { + logger.Printf("skipping: %s\n", path) } - + return true + } + // check if only a sub-folder of the path is excluded + if strings.Contains(path, p) { + if logger != nil { + logger.Printf("skipping: %s\n", path) + } + return true } } return false } - -/* -func (f fileList) Dump() { - for k, _ := range f.paths { - println(k) - } -} -*/ diff --git a/filelist_test.go b/filelist_test.go index eaa3cd6..a822c28 100644 --- a/filelist_test.go +++ b/filelist_test.go @@ -28,7 +28,7 @@ func Test_newFileList(t *testing.T) { name: "have paths", args: args{paths: []string{"*_test.go"}}, want: &fileList{patterns: map[string]struct{}{ - "*_test.go": struct{}{}, + ".*_test.go": struct{}{}, }}, }, } diff --git a/main.go b/main.go index 1f8d774..fcc455f 100644 --- a/main.go +++ b/main.go @@ -153,9 +153,9 @@ func main() { // Setup usage description flag.Usage = usage - // Exclude files - excluded := newFileList("*_test.go") - flag.Var(excluded, "skip", "File pattern to exclude from scan. Uses simple * globs and requires full or partial match") + // Exclude test files and files starting with _ or . + excluded := newFileList("*_test.go", "(^|.*[^a-zA-Z0-9_]+)?_[a-zA-Z0-9_]+.go", "(^|.*[^a-zA-Z0-9_]+)?\\.[a-zA-Z0-9_]+.go") + flag.Var(excluded, "skip", "File pattern to exclude from scan. Uses simple * globs or a regexp and requires full or partial match") incRules := "" flag.StringVar(&incRules, "include", "", "Comma separated list of rules IDs to include. (see rule list)") diff --git a/main_test.go b/main_test.go index b47a4b5..f39af22 100644 --- a/main_test.go +++ b/main_test.go @@ -36,6 +36,38 @@ func Test_shouldInclude(t *testing.T) { }, want: false, }, + { + name: ".go file starting with _ are excluded", + args: args{ + path: "_thing.go", + excluded: newFileList("(^|.*[^a-zA-Z0-9_]+)?_[a-zA-Z0-9_]+.go"), + }, + want: false, + }, + { + name: ".go file starting with _ in a path are excluded", + args: args{ + path: "/test/_thing.go", + excluded: newFileList("(^|.*[^a-zA-Z0-9_]+)?_[a-zA-Z0-9_]+.go"), + }, + want: false, + }, + { + name: ".go file starting with . are excluded", + args: args{ + path: ".thing.go", + excluded: newFileList("(^|.*[^a-zA-Z0-9_]+)?\\.[a-zA-Z0-9_]+.go"), + }, + want: false, + }, + { + name: ".go file starting with . in a path are excluded", + args: args{ + path: "/test/.thing.go", + excluded: newFileList("(^|.*[^a-zA-Z0-9_]+)?\\.[a-zA-Z0-9_]+.go"), + }, + want: false, + }, } for _, tt := range tests { if got := shouldInclude(tt.args.path, tt.args.excluded); got != tt.want { diff --git a/vendor.conf b/vendor.conf index 5f5b814..6c80cfb 100644 --- a/vendor.conf +++ b/vendor.conf @@ -4,4 +4,3 @@ github.com/GoAstScanner/gas # import github.com/GoASTScanner/gas cc52ef5 github.com/nbutton23/zxcvbn-go a22cb81 -github.com/ryanuber/go-glob v0.1