-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
walk.go
52 lines (42 loc) · 1.06 KB
/
walk.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
package walker
import (
"os"
"path/filepath"
"strings"
"github.com/bmatcuk/doublestar/v4"
"github.com/samber/lo"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
"github.com/aquasecurity/trivy/pkg/log"
)
const defaultSizeThreshold = int64(100) << 20 // 200MB
var defaultSkipDirs = []string{
"**/.git",
"proc",
"sys",
"dev",
}
type Option struct {
SkipFiles []string
SkipDirs []string
}
type WalkFunc func(filePath string, info os.FileInfo, opener analyzer.Opener) error
func CleanSkipPaths(skipPaths []string) []string {
return lo.Map(skipPaths, func(skipPath string, index int) string {
skipPath = filepath.ToSlash(filepath.Clean(skipPath))
return strings.TrimLeft(skipPath, "/")
})
}
func SkipPath(path string, skipPaths []string) bool {
path = strings.TrimLeft(path, "/")
// skip files
for _, pattern := range skipPaths {
match, err := doublestar.Match(pattern, path)
if err != nil {
return false // return early if bad pattern
} else if match {
log.Debug("Skipping path", log.String("path", path))
return true
}
}
return false
}