2019-08-19 15:28:24 +08:00
|
|
|
//+build !linux
|
|
|
|
|
2019-05-30 23:11:13 +01:00
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/fsnotify/fsnotify"
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/config/source"
|
2019-05-30 23:11:13 +01: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-20 22:32:47 +01:00
|
|
|
return nil, source.ErrWatcherStopped
|
2019-05-30 23:11:13 +01:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2020-08-07 01:21:09 +08:00
|
|
|
for {
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ARCH: Darwin Kernel Version 18.7.0
|
|
|
|
// ioutil.WriteFile truncates it before writing, but the problem is that
|
|
|
|
// you will receive two events(fsnotify.Chmod and fsnotify.Write).
|
|
|
|
// We can solve this problem by ignoring fsnotify.Chmod event.
|
|
|
|
if event.Op&fsnotify.Write != fsnotify.Write {
|
|
|
|
continue
|
2019-05-30 23:11:13 +01:00
|
|
|
}
|
|
|
|
|
2020-08-07 01:21:09 +08:00
|
|
|
c, err := w.f.Read()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
case err := <-w.fw.Errors:
|
2019-05-30 23:11:13 +01:00
|
|
|
return nil, err
|
2020-08-07 01:21:09 +08:00
|
|
|
case <-w.exit:
|
|
|
|
return nil, source.ErrWatcherStopped
|
2019-05-30 23:11:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *watcher) Stop() error {
|
|
|
|
return w.fw.Close()
|
|
|
|
}
|