-
Notifications
You must be signed in to change notification settings - Fork 479
/
watch.go
64 lines (55 loc) · 1.33 KB
/
watch.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
package file_match
import (
"os"
"path/filepath"
"github.com/go-kit/log"
"github.com/bmatcuk/doublestar"
"github.com/grafana/agent/component/discovery"
"github.com/grafana/agent/pkg/flow/logging/level"
)
// watch handles a single discovery.target for file watching.
type watch struct {
target discovery.Target
log log.Logger
}
func (w *watch) getPaths() ([]discovery.Target, error) {
allMatchingPaths := make([]discovery.Target, 0)
matches, err := doublestar.Glob(w.getPath())
if err != nil {
return nil, err
}
exclude := w.getExcludePath()
for _, m := range matches {
if exclude != "" {
if match, _ := doublestar.PathMatch(exclude, m); match {
continue
}
}
abs, err := filepath.Abs(m)
if err != nil {
level.Error(w.log).Log("msg", "error getting absolute path", "path", m, "err", err)
continue
}
fi, err := os.Stat(abs)
if err != nil {
level.Error(w.log).Log("msg", "error getting os stat", "path", abs, "err", err)
continue
}
if fi.IsDir() {
continue
}
dt := discovery.Target{}
for dk, v := range w.target {
dt[dk] = v
}
dt["__path__"] = abs
allMatchingPaths = append(allMatchingPaths, dt)
}
return allMatchingPaths, nil
}
func (w *watch) getPath() string {
return w.target["__path__"]
}
func (w *watch) getExcludePath() string {
return w.target["__path_exclude__"]
}