-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.go
119 lines (108 loc) · 2.46 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
package fswatcher
import (
"errors"
"github.com/fsnotify/fsnotify"
"os"
pathPkg "path"
"path/filepath"
"strings"
)
// Handler the fswatcher handler interface
type Handler interface {
CanHandle(path string) bool
Handle(ev *fsnotify.Event)
}
// ErrorHandler the fsnotify error handler
type ErrorHandler func(error)
// FileWatcher the file fswatcher struct
type FileWatcher struct {
watcher *fsnotify.Watcher
handlers []Handler
errorProcessor ErrorHandler
started bool
}
// AddWatch add path to watch
// path: the path to watch
// subDir: true to watch the sub directory; false to not watch the sub directory
func (fw *FileWatcher) AddWatch(path string, subDir bool) error {
path = pathPkg.Clean(strings.Replace(path, "\\", "/", -1))
stat, err := os.Stat(path)
if err != nil {
return err
}
err = fw.watcher.Add(path)
if subDir && stat.IsDir() {
filepath.Walk(path, func(p string, info os.FileInfo, er error) error {
if err != nil {
return nil
}
if er != nil {
err = er
return er
}
if info.IsDir() {
fw.AddWatch(p, false)
}
return nil
})
}
return err
}
// RemoveWatch remove the path from fswatcher. And then, the system will stop watching the changes.
func (fw *FileWatcher) RemoveWatch(path string) error {
return fw.watcher.Remove(path)
}
// AddHandler add file fswatcher handler
func (fw *FileWatcher) AddHandler(handler Handler) error {
if handler == nil {
return errors.New("the parameter 'handler' cannot be nil")
}
fw.handlers = append(fw.handlers, handler)
return nil
}
// SetErrorHandler set the fsnotify error handler
func (fw *FileWatcher) SetErrorHandler(h ErrorHandler) {
fw.errorProcessor = h
}
// Start star the file fswatcher
func (fw *FileWatcher) Start() {
if fw.started {
return
}
fw.started = true
go func() {
for {
select {
case ev := <-fw.watcher.Events:
ev.Name = strings.Replace(pathPkg.Clean(ev.Name), "\\", "/", -1)
for _, detector := range fw.handlers {
if detector.CanHandle(ev.Name) {
detector.Handle(&ev)
}
}
case err := <-fw.watcher.Errors:
if fw.errorProcessor != nil {
fw.errorProcessor(err)
}
}
if !fw.started {
break
}
}
}()
}
func (fw *FileWatcher) Stop() {
fw.watcher.Close()
fw.started = false
}
// NewWatcher create the new fswatcher
func NewWatcher() (*FileWatcher, error) {
t, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
w := &FileWatcher{
watcher: t,
}
return w, nil
}