Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/discovery/file/watch.go
4096 views
1
package file
2
3
import (
4
"os"
5
"path/filepath"
6
7
"github.com/go-kit/log"
8
9
"github.com/bmatcuk/doublestar"
10
"github.com/go-kit/log/level"
11
"github.com/grafana/agent/component/discovery"
12
)
13
14
// watch handles a single discovery.target for file watching.
15
type watch struct {
16
target discovery.Target
17
log log.Logger
18
}
19
20
func (w *watch) getPaths() ([]discovery.Target, error) {
21
allMatchingPaths := make([]discovery.Target, 0)
22
23
matches, err := doublestar.Glob(w.getPath())
24
if err != nil {
25
return nil, err
26
}
27
exclude := w.getExcludePath()
28
for _, m := range matches {
29
if exclude != "" {
30
if match, _ := doublestar.PathMatch(exclude, m); match {
31
continue
32
}
33
}
34
abs, err := filepath.Abs(m)
35
if err != nil {
36
level.Error(w.log).Log("msg", "error getting absolute path", "path", m, "err", err)
37
continue
38
}
39
fi, err := os.Stat(abs)
40
if err != nil {
41
level.Error(w.log).Log("msg", "error getting os stat", "path", abs, "err", err)
42
continue
43
}
44
if fi.IsDir() {
45
continue
46
}
47
dt := discovery.Target{}
48
for dk, v := range w.target {
49
dt[dk] = v
50
}
51
dt["__path__"] = abs
52
allMatchingPaths = append(allMatchingPaths, dt)
53
}
54
55
return allMatchingPaths, nil
56
}
57
58
func (w *watch) getPath() string {
59
return w.target["__path__"]
60
}
61
62
func (w *watch) getExcludePath() string {
63
return w.target["__path_exclude__"]
64
}
65
66