File tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed
Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -42,6 +42,8 @@ func New(options *Options) (*Runner, error) {
4242 if err != nil {
4343 return nil , err
4444 }
45+ watchFile (r .options .RulesFile , serverTCP .LoadTemplate )
46+
4547 r .serverTCP = serverTCP
4648 return & r , nil
4749 }
Original file line number Diff line number Diff line change 1+ package runner
2+
3+ import (
4+ "log"
5+
6+ "github.com/fsnotify/fsnotify"
7+ )
8+
9+ type WatchEvent func (fname string ) error
10+
11+ func watchFile (fname string , callback WatchEvent ) (watcher * fsnotify.Watcher , err error ) {
12+ watcher , err = fsnotify .NewWatcher ()
13+ if err != nil {
14+ return
15+ }
16+ go func () {
17+ for {
18+ select {
19+ case event , ok := <- watcher .Events :
20+ if ! ok {
21+ continue
22+ }
23+ if event .Op & fsnotify .Write == fsnotify .Write {
24+ if err := callback (fname ); err != nil {
25+ log .Println ("err" , err )
26+ }
27+ }
28+ case <- watcher .Errors :
29+ // ignore errors for now
30+ }
31+ }
32+ }()
33+
34+ err = watcher .Add (fname )
35+ return
36+ }
You can’t perform that action at this time.
0 commit comments