-
Notifications
You must be signed in to change notification settings - Fork 4
/
watcher.go
159 lines (143 loc) · 2.86 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
157
158
159
package generator
import (
"context"
"log"
"os"
"path/filepath"
"strings"
"sync"
"github.com/fsnotify/fsnotify"
"golang.org/x/sync/errgroup"
)
type Watcher struct {
isWorking bool
isWorkingMu sync.RWMutex
eventCh chan fsnotify.Event
handler func(context.Context, fsnotify.Event)
watcher *fsnotify.Watcher
}
func NewWatcher() (*Watcher, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
return &Watcher{
eventCh: make(chan fsnotify.Event),
handler: func(context.Context, fsnotify.Event) {},
watcher: watcher,
}, nil
}
func (w *Watcher) Close() {
w.watcher.Close()
}
func (w *Watcher) SetHandler(handler func(ctx context.Context, event fsnotify.Event)) {
w.handler = handler
}
func (w *Watcher) SetWatchPath(path ...string) error {
return w.setWatchPathRecursive(path...)
}
func (w *Watcher) setWatchPathRecursive(path ...string) error {
pathMap := make(map[string]struct{})
for _, p := range path {
if err := filepath.Walk(p, func(path string, info os.FileInfo, err error) error {
if info == nil {
return nil
}
if !info.IsDir() {
return nil
}
if strings.HasPrefix(path, ".") {
return nil
}
pathMap[path] = struct{}{}
return nil
}); err != nil {
return err
}
}
for path := range pathMap {
if err := w.watcher.Add(path); err != nil {
return err
}
}
return nil
}
func (w *Watcher) Run(ctx context.Context) error {
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error {
w.sendEventLoop()
return nil
})
eg.Go(func() error {
w.receiveEventLoop(ctx)
return nil
})
if err := eg.Wait(); err != nil {
return err
}
return nil
}
func (w *Watcher) sendEventLoop() {
for {
select {
case event, ok := <-w.watcher.Events:
if !ok {
continue
}
w.handleEvent(event)
case err, ok := <-w.watcher.Errors:
if !ok {
continue
}
log.Println("error:", err)
}
}
}
func (w *Watcher) handleEvent(event fsnotify.Event) {
if w.IsWorking() {
return
}
if !w.isModified(event) {
return
}
if w.ignoreFilePath(event.Name) {
return
}
w.eventCh <- event
}
func (w *Watcher) isModified(ev fsnotify.Event) bool {
return ev.Has(fsnotify.Write) ||
ev.Has(fsnotify.Create) ||
ev.Has(fsnotify.Remove) ||
ev.Has(fsnotify.Rename)
}
func (w *Watcher) ignoreFilePath(path string) bool {
if strings.HasPrefix(path, "#") {
return true
}
if strings.HasPrefix(path, ".") {
return true
}
if filepath.Ext(path) != ".proto" {
return true
}
return false
}
func (w *Watcher) receiveEventLoop(ctx context.Context) {
for {
event := <-w.eventCh
w.setWorking(true)
w.handler(ctx, event)
w.setWorking(false)
}
}
func (w *Watcher) IsWorking() bool {
w.isWorkingMu.RLock()
defer w.isWorkingMu.RUnlock()
return w.isWorking
}
func (w *Watcher) setWorking(working bool) {
w.isWorkingMu.Lock()
defer w.isWorkingMu.Unlock()
w.isWorking = working
}