-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection.go
123 lines (103 loc) · 2.39 KB
/
collection.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"archive/zip"
"bytes"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/gobwas/glob"
)
type Matcher func(string) bool
func NewStaticMatcher(pattern string) Matcher {
return func(filename string) bool {
return pattern == filename
}
}
func NewGlobMatcher(pattern string) Matcher {
pattern = strings.ReplaceAll(pattern, "\\", "\\\\")
m, _ := glob.Compile(pattern)
return m.Match
}
func NewRegexpMatcher(pattern string) Matcher {
re, _ := regexp.Compile(pattern)
return re.MatchString
}
func LoadMatchers(cfg Configuration) ([]Matcher, []string, error) {
// parse collector configuration
if cfg.QuackTargets != "" {
fh, err := os.Open(cfg.QuackTargets)
if err != nil {
return nil, nil, err
}
return ParseQuack(fh)
} else if cfg.KapeTargets != "" {
return ParseKapeTargets(cfg.KapeTargets, cfg.KapeFiles)
} else {
r := bytes.NewReader(defaultQuack)
return ParseQuack(r)
}
}
func GetPaths(cfg Configuration) ([]string, error) {
scanned := 0
matchers, paths, err := LoadMatchers(cfg)
if err != nil {
return nil, fmt.Errorf("load matchers: %w", err)
}
roots := cfg.CollectionRoots
if len(roots) == 0 {
roots = DefaulRootPaths()
}
for _, root := range roots {
err = filepath.WalkDir(root, func(path string, info fs.DirEntry, err error) error {
if err != nil {
WarnLogger.Printf("traverse | %v", err)
return fs.SkipDir
}
pathTrimmed := strings.TrimPrefix(path, root)
// InfoLogger.Printf("traverse | trimmed path: %s -> %s", path, pathTrimmed)
scanned++
if !info.IsDir() {
for _, match := range matchers {
if match(path) || match(pathTrimmed) {
paths = append(paths, path)
break
}
}
}
return nil
})
if err != nil {
return paths, err
}
}
InfoLogger.Printf("traverse | scanned %d paths, resulted in %d files to collect", scanned, len(paths))
return paths, err
}
func CollectFile(cfg Configuration, archive *zip.Writer, path string) error {
rel, _ := filepath.Rel(filepath.VolumeName(path)+"/", filepath.ToSlash(path))
r, err := os.Open(path)
if err != nil {
return err
}
defer r.Close()
fi, err := r.Stat()
if err != nil {
return err
}
fh, err := zip.FileInfoHeader(fi)
if err != nil {
return err
}
fh.Name = rel
fh.Method = zip.Deflate
w, err := archive.CreateHeader(fh)
if err != nil {
return err
}
_, err = io.Copy(w, r)
return err
}