-
Notifications
You must be signed in to change notification settings - Fork 2
/
watch.go
109 lines (94 loc) · 2.54 KB
/
watch.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
package main
import (
"fmt"
"path/filepath"
"github.com/e-XpertSolutions/f5-rest-client/f5"
fsnotify "gopkg.in/fsnotify.v1"
)
type watchEvent fsnotify.Event
func (e watchEvent) isCreate() bool {
return e.Op&fsnotify.Create == fsnotify.Create
}
func (e watchEvent) isWrite() bool {
return e.Op&fsnotify.Write == fsnotify.Write
}
func (e watchEvent) isRemove() bool {
return e.Op&fsnotify.Remove == fsnotify.Remove
}
func (e watchEvent) isRename() bool {
return e.Op&fsnotify.Rename == fsnotify.Rename
}
func (e watchEvent) isChmod() bool {
return e.Op&fsnotify.Chmod == fsnotify.Chmod
}
type watchRoutine struct {
watcher *fsnotify.Watcher
stopCh chan struct{}
}
func watchDir(f5Client *f5.Client, l logger, cfg watchConfig) (*watchRoutine, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
stopCh := make(chan struct{})
go func() {
for {
select {
case event := <-watcher.Events:
e := watchEvent(event)
if e.isChmod() {
continue
}
fmt.Printf("testing %q against %v", e.Name, cfg.Exclude)
if isExcluded(filepath.Base(e.Name), cfg.Exclude) {
l.Noticef("skipping %q due to an exclusion pattern defined in the configuration file", e.Name)
continue
}
tx, err := f5Client.Begin()
if err != nil {
l.Errorf("cannot start f5 transaction for file %q", e.Name)
continue
}
switch {
case e.isCreate():
l.Noticef("event received %q for file %q", "CREATE", e.Name)
err = uploadNewFile(tx, filepath.Base(e.Name), e.Name)
case e.isWrite():
l.Noticef("event received %q for file %q", "WRITE", e.Name)
err = uploadExistingFile(tx, filepath.Base(e.Name), e.Name)
case e.isRename():
l.Noticef("event received %q for file %q", "RENAME", e.Name)
err = deleteFile(tx, filepath.Base(e.Name))
case e.isRemove():
if !cfg.RemoveRemoveFiles {
continue
}
l.Noticef("event received %q for file %q", "REMOVE", e.Name)
err = deleteFile(tx, filepath.Base(e.Name))
}
if err != nil {
l.Errorf("cannot upload file %q: %v", e.Name, err)
continue
}
if err := tx.Commit(); err != nil {
l.Errorf("cannot commit f5 transaction for file %q: %v", e.Name, err)
}
case err := <-watcher.Errors:
l.Error("watcher error: ", err)
case <-stopCh:
return
}
}
}()
if err := watcher.Add(cfg.Dir); err != nil {
stopCh <- struct{}{}
return nil, err
}
return &watchRoutine{
watcher: watcher,
stopCh: stopCh,
}, nil
}
func (wr *watchRoutine) stop() error {
return wr.watcher.Close()
}