-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
174 lines (139 loc) · 3.21 KB
/
file.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
package watcher
import (
"context"
"errors"
"log"
"os"
"path/filepath"
"sync"
"time"
"github.com/mickyco94/saucisson/internal/config"
filewatcher "github.com/radovskyb/watcher"
"github.com/sirupsen/logrus"
)
var (
ErrWatchCreateExistingFile = errors.New("Cannot watch for creation of a file that already exists")
)
var operationMap = map[config.Operation]filewatcher.Op{
config.Create: filewatcher.Create,
config.Remove: filewatcher.Remove,
config.Rename: filewatcher.Rename,
config.Update: filewatcher.Write,
}
func NewFile(logger logrus.FieldLogger) *File {
watcher := filewatcher.New()
watcher.IgnoreHiddenFiles(false)
return &File{
runningMu: sync.Mutex{},
isRunning: false,
close: make(chan struct{}),
done: make(chan struct{}),
logger: logger,
watcher: watcher,
}
}
type fileEntry struct {
//path is the full path of the file/directory being watched
path string
//dir is set to true if the specified entry is a watch for a directory
dir bool
//op is the type of operations we are listening for
op filewatcher.Op
//handler will be executed when a match is found
handler func()
}
type File struct {
runningMu sync.Mutex
isRunning bool
close chan struct{}
done chan struct{}
logger logrus.FieldLogger
entries []fileEntry
watcher *filewatcher.Watcher
}
func (file *File) Stop(ctx context.Context) error {
file.runningMu.Lock()
defer file.runningMu.Unlock()
if !file.isRunning {
return nil
}
file.close <- struct{}{}
select {
case <-ctx.Done():
return ctx.Err()
case <-file.done:
return nil
}
}
// HandleFunc registers the provided function to be executed, when the provided
// condition has been satisfied.
// An error is returned if the provided condition is not logically complete
func (f *File) HandleFunc(condition *config.File, handler func()) error {
file, err := os.Stat(condition.Path)
if err != nil {
return err
}
if file != nil &&
!file.IsDir() &&
operationMap[condition.Operation] == filewatcher.Create {
return ErrWatchCreateExistingFile
}
err = f.watcher.Add(condition.Path)
if err != nil {
return err
}
f.entries = append(f.entries, fileEntry{
path: condition.Path,
dir: file.IsDir(),
op: operationMap[condition.Operation],
handler: handler,
})
return nil
}
func (entry fileEntry) matches(event filewatcher.Event) bool {
if event.Op != entry.op {
return false
}
if event.Path == entry.path {
return true
}
if entry.op == filewatcher.Rename && event.OldPath == entry.path {
return true
}
if entry.dir && entry.path == filepath.Dir(event.Path) {
return true
}
return false
}
func (file *File) Run(pollingInterval time.Duration) error {
file.runningMu.Lock()
if file.isRunning {
file.runningMu.Unlock()
return nil
}
go func() {
defer func() {
file.done <- struct{}{}
}()
for {
select {
case <-file.close:
return
case event, open := <-file.watcher.Event:
if !open {
return
}
for _, entry := range file.entries {
if entry.matches(event) {
entry.handler()
}
}
case err := <-file.watcher.Error:
log.Printf("error: %v\n", err)
}
}
}()
file.isRunning = true
file.runningMu.Unlock()
return file.watcher.Start(pollingInterval)
}