2019-08-19 10:28:24 +03:00
|
|
|
//+build !linux
|
|
|
|
|
2019-05-31 01:11:13 +03:00
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/fsnotify/fsnotify"
|
2020-07-27 15:22:00 +03:00
|
|
|
"github.com/micro/go-micro/v3/config/source"
|
2019-05-31 01:11:13 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type watcher struct {
|
|
|
|
f *file
|
|
|
|
|
|
|
|
fw *fsnotify.Watcher
|
|
|
|
exit chan bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func newWatcher(f *file) (source.Watcher, error) {
|
|
|
|
fw, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fw.Add(f.path)
|
|
|
|
|
|
|
|
return &watcher{
|
|
|
|
f: f,
|
|
|
|
fw: fw,
|
|
|
|
exit: make(chan bool),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *watcher) Next() (*source.ChangeSet, error) {
|
|
|
|
// is it closed?
|
|
|
|
select {
|
|
|
|
case <-w.exit:
|
2019-08-21 00:32:47 +03:00
|
|
|
return nil, source.ErrWatcherStopped
|
2019-05-31 01:11:13 +03:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
// try get the event
|
|
|
|
select {
|
|
|
|
case event, _ := <-w.fw.Events:
|
|
|
|
if event.Op == fsnotify.Rename {
|
|
|
|
// check existence of file, and add watch again
|
|
|
|
_, err := os.Stat(event.Name)
|
|
|
|
if err == nil || os.IsExist(err) {
|
|
|
|
w.fw.Add(event.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := w.f.Read()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
case err := <-w.fw.Errors:
|
|
|
|
return nil, err
|
|
|
|
case <-w.exit:
|
2019-08-21 00:32:47 +03:00
|
|
|
return nil, source.ErrWatcherStopped
|
2019-05-31 01:11:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *watcher) Stop() error {
|
|
|
|
return w.fw.Close()
|
|
|
|
}
|