Skip to content

Commit e615365

Browse files
committed
add livereloading for rule config
1 parent 4fb2abb commit e615365

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

internal/runner/runner.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

internal/runner/watchdog.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
}

0 commit comments

Comments
 (0)