/
exclusion.go
70 lines (61 loc) · 2.01 KB
/
exclusion.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package filesystem
import (
"fmt"
"regexp"
"github.com/ARM-software/golang-utils/utils/commonerrors"
"github.com/ARM-software/golang-utils/utils/reflection"
)
func ExcludeFiles(files []string, regexes []*regexp.Regexp) (cleansedList []string, err error) {
cleansedList = []string{}
for i := range files {
f := files[i]
if !IsPathExcluded(f, regexes...) {
cleansedList = append(cleansedList, f)
}
}
return
}
func NewExclusionRegexList(pathSeparator rune, exclusionPatterns ...string) ([]*regexp.Regexp, error) {
var regexes []*regexp.Regexp
var patternsExtendedList []string
for i := range exclusionPatterns {
pattern := exclusionPatterns[i]
if !reflection.IsEmpty(pattern) {
patternsExtendedList = append(patternsExtendedList, pattern, fmt.Sprintf(".*/%v/.*", pattern), fmt.Sprintf(".*%v%v%v.*", pathSeparator, pattern, pathSeparator))
}
}
for i := range patternsExtendedList {
r, err := regexp.Compile(patternsExtendedList[i])
if err != nil {
return nil, fmt.Errorf("%w: could not compile pattern [%v]: %v", commonerrors.ErrInvalid, patternsExtendedList[i], err.Error())
}
regexes = append(regexes, r)
}
return regexes, nil
}
func IsPathExcludedFromPatterns(path string, pathSeparator rune, exclusionPatterns ...string) bool {
regexes, err := NewExclusionRegexList(pathSeparator, exclusionPatterns...)
if err != nil {
return false
}
return IsPathExcluded(path, regexes...)
}
func IsPathExcluded(path string, exclusionPatterns ...*regexp.Regexp) bool {
for i := range exclusionPatterns {
if exclusionPatterns[i].MatchString(path) {
return true
}
}
return false
}
// ExcludeAll excludes files
func ExcludeAll(files []string, exclusionPatterns ...string) ([]string, error) {
return globalFileSystem.ExcludeAll(files, exclusionPatterns...)
}
func (fs *VFS) ExcludeAll(files []string, exclusionPatterns ...string) ([]string, error) {
regexes, err := NewExclusionRegexList(fs.PathSeparator(), exclusionPatterns...)
if err != nil {
return nil, err
}
return ExcludeFiles(files, regexes)
}