-
Notifications
You must be signed in to change notification settings - Fork 532
/
watcher.go
156 lines (133 loc) · 2.99 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package localfs
import (
"context"
"errors"
"os"
"path/filepath"
"strings"
"sync"
"time"
wt "github.com/radovskyb/watcher"
)
type watcher struct {
wt *wt.Watcher
workdir string
ignoreHidden bool
ignoreFolders bool
ignoreExts []string
onChange func()
interval time.Duration
ctx context.Context
done *sync.WaitGroup
}
// WatcherOption used to configure watcher.
type WatcherOption func(*watcher)
// WatcherWorkdir to set as a root to paths needs to be watched.
func WatcherWorkdir(path string) WatcherOption {
return func(w *watcher) {
w.workdir = path
}
}
// WatcherOnChange sets a hook that executed on every change on filesystem.
func WatcherOnChange(hook func()) WatcherOption {
return func(w *watcher) {
w.onChange = hook
}
}
// WatcherPollingInterval overwrites default polling interval to check filesystem changes.
func WatcherPollingInterval(d time.Duration) WatcherOption {
return func(w *watcher) {
w.interval = d
}
}
// WatcherIgnoreHidden ignores hidden(dot) files.
func WatcherIgnoreHidden() WatcherOption {
return func(w *watcher) {
w.ignoreHidden = true
}
}
func WatcherIgnoreFolders() WatcherOption {
return func(w *watcher) {
w.ignoreFolders = true
}
}
// WatcherIgnoreExt ignores files with matching file extensions.
func WatcherIgnoreExt(exts ...string) WatcherOption {
return func(w *watcher) {
w.ignoreExts = exts
}
}
// Watch starts watching changes on the paths. options are used to configure the
// behaviour of watch operation.
func Watch(ctx context.Context, paths []string, options ...WatcherOption) error {
w := &watcher{
wt: wt.New(),
onChange: func() {},
interval: time.Millisecond * 300,
done: &sync.WaitGroup{},
ctx: ctx,
}
w.wt.SetMaxEvents(1)
for _, o := range options {
o(w)
}
w.wt.AddFilterHook(func(info os.FileInfo, fullPath string) error {
if info.IsDir() && w.ignoreFolders {
return wt.ErrSkip
}
if w.isFileIgnored(fullPath) {
return wt.ErrSkip
}
return nil
})
// ignore hidden paths.
w.wt.IgnoreHiddenFiles(w.ignoreHidden)
// add paths to watch
if err := w.addPaths(paths...); err != nil {
return err
}
// start watching.
w.done.Add(1)
go w.listen()
if err := w.wt.Start(w.interval); err != nil {
return err
}
w.done.Wait()
return nil
}
func (w *watcher) listen() {
defer w.done.Done()
for {
select {
case <-w.wt.Event:
w.onChange()
case <-w.wt.Closed:
return
case <-w.ctx.Done():
w.wt.Close()
}
}
}
func (w *watcher) addPaths(paths ...string) error {
for _, path := range paths {
if !filepath.IsAbs(path) {
path = filepath.Join(w.workdir, path)
}
// Ignoring paths that don't exist
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
continue
}
if err := w.wt.AddRecursive(path); err != nil {
return err
}
}
return nil
}
func (w *watcher) isFileIgnored(path string) bool {
for _, ext := range w.ignoreExts {
if strings.HasSuffix(path, ext) {
return true
}
}
return false
}