gosuki/watcher.go

42 lines
1.0 KiB
Go
Raw Normal View History

2017-10-20 10:51:56 +00:00
package main
import (
"github.com/fsnotify/fsnotify"
)
2017-11-20 15:10:11 +00:00
type IWatchable interface {
2017-11-20 18:07:15 +00:00
SetupWatcher() // Starts watching bookmarks and runs Load on change
2017-11-20 15:12:20 +00:00
Watch() bool // starts watching linked watcher
2017-11-20 18:07:15 +00:00
Run() // Callback fired on event
2017-11-20 15:10:11 +00:00
Watcher() *fsnotify.Watcher // returns linked watcher
GetPath() string // returns watched path
GetDir() string // returns watched dir
}
2017-11-20 15:05:44 +00:00
func WatcherThread(w IWatchable) {
2017-11-20 15:05:44 +00:00
bookmarkPath := w.GetPath()
2017-11-19 16:00:37 +00:00
log.Infof("watching %s", bookmarkPath)
2017-11-20 15:05:44 +00:00
watcher := w.Watcher()
for {
select {
2017-11-20 15:05:44 +00:00
case event := <-watcher.Events:
if event.Op&fsnotify.Create == fsnotify.Create &&
event.Name == bookmarkPath {
2017-10-20 10:51:56 +00:00
2017-11-16 14:43:09 +00:00
debugPrint("event: %v | eventName: %v", event.Op, event.Name)
2017-11-17 15:58:16 +00:00
//debugPrint("modified file: %s", event.Name)
//start := time.Now()
2017-11-20 15:05:44 +00:00
//parseFunc(bw)
2017-11-20 18:07:15 +00:00
w.Run()
2017-11-17 15:58:16 +00:00
//elapsed := time.Since(start)
//debugPrint("parsed in %s", elapsed)
2017-10-20 10:51:56 +00:00
}
2017-11-20 15:05:44 +00:00
case err := <-watcher.Errors:
2017-11-19 16:00:37 +00:00
log.Errorf("error: %s", err)
2017-10-20 10:51:56 +00:00
}
}
}