-
Notifications
You must be signed in to change notification settings - Fork 6
/
watcher.go
111 lines (100 loc) · 2.45 KB
/
watcher.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
package main
import (
"log"
"os"
"regexp"
"sort"
"github.com/fsnotify/fsnotify"
"fmt"
"path/filepath"
)
var watcherLog = NewLogFunc("watcher")
// Watcher file wather struct
type Watcher struct {
w *fsnotify.Watcher
WatchDirOnly bool
TargetDirs []string
ExcludeDirs []string
TargetFileExt []string
ExcludeFilePatterns []string
}
// Close close watcher
func (w *Watcher) Close() {
w.w.Close()
}
func contains(v string, l []string) bool {
sort.Strings(l)
i := sort.SearchStrings(l, v)
if i < len(l) && l[i] == v {
return true
}
return false
}
func matchContains(v string, excludeStrs []string) bool {
for _, es := range excludeStrs {
match, err := filepath.Match(es, v)
if err != nil {
log.Fatal(err)
}
if match {
return true
}
}
return false
}
// initWatcher add watch target files to watcher
func (w *Watcher) initWatcher() {
var excludeDirRegexps []*regexp.Regexp
for _, excludeDirStr := range w.ExcludeDirs {
r := regexp.MustCompile(excludeDirStr)
excludeDirRegexps = append(excludeDirRegexps, r)
}
for _, targetDir := range w.TargetDirs {
filepath.Walk(targetDir, func(path string, info os.FileInfo, err error) error {
for _, e := range excludeDirRegexps {
if e.MatchString(path) {
return nil
}
}
if w.WatchDirOnly {
watcherLog(fmt.Sprintf("start watching dir %s", path))
err := w.w.Add(path)
if err != nil {
watcherLog(fmt.Sprintf("failed to watch dir: %s: %s", path, err))
return err
}
return nil
}
fileExt := filepath.Ext(path)
if contains(fileExt, w.TargetFileExt) && !matchContains(path, w.ExcludeFilePatterns) {
watcherLog(fmt.Sprintf("start watching fle %s", path))
err := w.w.Add(path)
if err != nil {
watcherLog(fmt.Sprintf("failed to watch file: %s: %s", path, err))
return err
}
}
return nil
})
}
return
}
// NewWatcher create target file watcher
func NewWatcher(config *Config) (*Watcher, error) {
var watcher *Watcher
w, err := fsnotify.NewWatcher()
if err != nil {
watcherLog(fmt.Sprintf("failed to create watcher: %s", err))
return watcher, err
}
watcher = &Watcher{
w: w,
WatchDirOnly: config.WatchDirOnly,
TargetDirs: config.WatchTargetDirs,
ExcludeDirs: config.WatchExcludeDirs,
TargetFileExt: config.WatchFileExt,
ExcludeFilePatterns: config.WatchFileExcludePatterns,
}
watcher.initWatcher()
return watcher, nil
}