Skip to content

Commit eb3cd44

Browse files
authored
Feature/updated filters (#143)
* Inclusions and exclusions are no provided with the '-g' command. * Inclusion and exclusion filters are now provided with the same '-g' option.
1 parent 0a264e2 commit eb3cd44

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

internal/commands/scan.go

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,32 @@ func compressFolder(sourceDir string, filter string, sourceExclusionFilter strin
184184
return outputFile.Name(), nil
185185
}
186186

187-
func filterMatched(filters []string, fileName string) bool {
187+
func filterMatched(filters []string, fileName string) (bool, bool) {
188+
var matched = true
189+
var firstMatch = true
190+
var excluded = false
188191
for _, filter := range filters {
189-
matched, _ := path.Match(filter, fileName)
190-
if matched {
191-
return true
192+
if filter[0] == '!' {
193+
// it just needs to match one exclusion to be excluded.
194+
if !excluded {
195+
excluded, _ = path.Match(filter[1:], fileName)
196+
}
197+
} else {
198+
// 1. If there are no inclusions everythng is considered included
199+
// 2. If there is at least one included and nothing matches the its
200+
// not included
201+
// it just needs to match one thing to be matched
202+
if firstMatch {
203+
// Need to reset the match flag because now we need to find one.
204+
matched = false
205+
firstMatch = false
206+
}
207+
if !matched {
208+
matched, _ = path.Match(filter, fileName)
209+
}
192210
}
193211
}
194-
return false
212+
return matched, excluded
195213
}
196214

197215
func addDirFiles(zip *zip.Writer, baseDir string, parentDir string, filters []string, exclusions []string) {
@@ -205,11 +223,12 @@ func addDirFiles(zip *zip.Writer, baseDir string, parentDir string, filters []st
205223
var matched = true
206224
var excluded = false
207225
if filters != nil {
208-
matched = filterMatched(filters, file.Name())
209-
}
210-
if exclusions != nil {
211-
excluded = filterMatched(exclusions, file.Name())
226+
matched, excluded = filterMatched(filters, file.Name())
212227
}
228+
// Exclusions have been moved to filters list
229+
//if exclusions != nil {
230+
// excluded = filterMatched(exclusions, file.Name())
231+
//}
213232
if matched && !excluded {
214233
fmt.Println("Included: ", fileName)
215234
dat, err := ioutil.ReadFile(parentDir + file.Name())

0 commit comments

Comments
 (0)