-
Notifications
You must be signed in to change notification settings - Fork 1
/
notify.go
258 lines (217 loc) · 4.8 KB
/
notify.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package fsnotify
import (
"github.com/fsnotify/fsnotify"
"github.com/kyaxcorp/go-core/core/helpers/errors2"
"github.com/kyaxcorp/go-core/core/helpers/filesystem"
"log"
"path/filepath"
"strings"
"time"
)
type Op fsnotify.Op
// Just extending!
// These are the operations of an event!
const (
Create Op = 1 << iota
Write
Remove
Rename
Chmod
)
// This is the Event structure when we receive from the watcher
type EventData struct {
// This is the path where it happened!
Path string
// This is the time when happened the event
Time time.Time
// This is the filtered Name
Name string
// This is the operation that happened
Op Op
}
type WatchCallback func(e EventData)
// This is the structure when we send the configuration to the watcher!
type WatchConfig struct {
CallbackGoRoutine bool
// This is the callback which should be called!
Callback WatchCallback
// What operations should listen on!
Op []Op
}
type OnError func(err error)
type Notifier struct {
// The watcher
watcher *fsnotify.Watcher
// The paths that are being watched
watchPaths map[string]WatchConfig
status bool
// If is there any path in the stack
isAnyPathAdded bool
// On Error Callback
onError OnError
}
func New() (*Notifier, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
return nil, err
}
return &Notifier{
// Attach the watcher
watcher: watcher,
// Create the map!
watchPaths: make(map[string]WatchConfig),
}, nil
}
func (n *Notifier) IsStarted() bool {
return n.status
}
func (n *Notifier) OnError(callback OnError) *Notifier {
// Even nil is accepted!
n.onError = callback
return n
}
func (n *Notifier) IsStopped() bool {
return !n.status
}
func (n *Notifier) checkNrOfWatchPaths() {
if len(n.watchPaths) > 0 {
n.isAnyPathAdded = true
} else {
n.isAnyPathAdded = false
}
}
func (n *Notifier) Watch(
path string,
watchConfig WatchConfig,
) *Notifier {
if path != "" {
path, _ = filesystem.RealPath(path)
}
// Add the path
if path == "" || watchConfig.Callback == nil {
errors2.New(0, "FsNotify - Path empty or callback")
return n
}
// Check if exists already!
//We can overwrite the existing callback!
if _, ok := n.watchPaths[path]; !ok {
// Exists!
// Add to watcher
_err := n.watcher.Add(path)
if _err != nil {
errors2.New(0, _err.Error())
return n
}
}
// Write or overwrite the callback!
n.watchPaths[path] = watchConfig
n.checkNrOfWatchPaths()
return n
}
// Remove the watcher!
func (n *Notifier) Remove(path string) *Notifier {
if _, ok := n.watchPaths[path]; ok {
_err := n.watcher.Remove(path)
if _err != nil {
// log.Println("Failed removing", _err)
errors2.New(0, "Failed removing -> "+_err.Error())
return n
}
delete(n.watchPaths, path)
n.checkNrOfWatchPaths()
}
return n
}
func (n *Notifier) Start() *Notifier {
if n.IsStarted() {
log.Println("notifier already started")
return n
}
go func() {
// Entering infinite loop
// TODO: we should do a finished variable!
for {
select {
case event, ok := <-n.watcher.Events:
// Getting events through Channel
if !ok {
return
}
// If are any paths...
if !n.isAnyPathAdded {
return
}
for path, watchConfig := range n.watchPaths {
if !strings.Contains(event.Name, path) {
continue
}
runCallback := false
if len(watchConfig.Op) > 0 {
for _, op := range watchConfig.Op {
if Op(event.Op) == op {
runCallback = true
break
}
}
} else {
// It's everything! Listening to all!
runCallback = true
}
if !runCallback {
// Skipping this path which is being watched
continue
}
//log.Println("aaa", event.Op, Op(event.Op))
ev := EventData{
Path: event.Name,
Time: time.Now(),
Name: filepath.Base(event.Name), // Filter the file Name!
Op: Op(event.Op),
}
if watchConfig.CallbackGoRoutine {
go watchConfig.Callback(ev)
} else {
watchConfig.Callback(ev)
}
}
/*log.Println("event:", event)
if event.Op&fsnotify.Write == fsnotify.Write {
log.Println("modified file:", event.Name)
}*/
}
}
log.Println("Notifier goroutine stopped!")
}()
// On Errors
go func() {
// Entering infinite loop
// TODO: we should do a finished variable!
for {
select {
case err, ok := <-n.watcher.Errors:
// Getting events through Channel
if !ok {
return
}
// Callback!
if n.onError != nil {
n.onError(err)
}
log.Println("error:", err)
}
}
log.Println("Notifier goroutine stopped!")
}()
return n
}
func (n *Notifier) Stop() *Notifier {
if n.IsStopped() {
log.Println("notifier already stopped!")
return n
}
// TODO: stop the created goroutines!
n.watcher.Close()
log.Println("notifier stopped")
return n
}