From 3ae2762bb1a6d9d2f222205f70ebf3fa881cef12 Mon Sep 17 00:00:00 2001 From: Cosmin Cojocar Date: Mon, 10 Apr 2017 11:18:02 +0200 Subject: [PATCH] Add support for partial path match in the skip option --- filelist.go | 20 +++++++++++++++----- filelist_test.go | 6 ++++++ main.go | 2 +- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/filelist.go b/filelist.go index 58ecb5d..ac767ed 100644 --- a/filelist.go +++ b/filelist.go @@ -57,14 +57,24 @@ func (f *fileList) Set(path string) error { func (f fileList) Contains(path string) bool { for p := range f.patterns { - if glob.Glob(p, path) { - if logger != nil { - logger.Printf("skipping: %s\n", path) + if strings.Contains(p, glob.GLOB) { + if glob.Glob(p, path) { + if logger != nil { + logger.Printf("skipping: %s\n", path) + } + return true } - return true + } 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 + } + } } - //log.Printf("including: %s\n", path) return false } diff --git a/filelist_test.go b/filelist_test.go index dd0f296..eaa3cd6 100644 --- a/filelist_test.go +++ b/filelist_test.go @@ -235,6 +235,12 @@ func Test_fileList_Contains(t *testing.T) { args: args{path: "/baz/bar/foo_test.go"}, want: true, }, + { + name: "sub-folder, match", + fields: fields{patterns: []string{"vendor"}}, + args: args{path: "/baz/vendor/bar/foo_test.go"}, + want: true, + }, } for _, tt := range tests { f := newFileList(tt.fields.patterns...) diff --git a/main.go b/main.go index 7922cf9..1f8d774 100644 --- a/main.go +++ b/main.go @@ -155,7 +155,7 @@ func main() { // Exclude files excluded := newFileList("*_test.go") - flag.Var(excluded, "skip", "File pattern to exclude from scan. Uses simple * globs and requires full match") + flag.Var(excluded, "skip", "File pattern to exclude from scan. Uses simple * globs and requires full or partial match") incRules := "" flag.StringVar(&incRules, "include", "", "Comma separated list of rules IDs to include. (see rule list)")