github.com/reds/docker@v1.11.2-rc1/pkg/filenotify/filenotify.go (about) 1 // Package filenotify provides a mechanism for watching file(s) for changes. 2 // Generally leans on fsnotify, but provides a poll-based notifier which fsnotify does not support. 3 // These are wrapped up in a common interface so that either can be used interchangeably in your code. 4 package filenotify 5 6 import "gopkg.in/fsnotify.v1" 7 8 // FileWatcher is an interface for implementing file notification watchers 9 type FileWatcher interface { 10 Events() <-chan fsnotify.Event 11 Errors() <-chan error 12 Add(name string) error 13 Remove(name string) error 14 Close() error 15 } 16 17 // New tries to use an fs-event watcher, and falls back to the poller if there is an error 18 func New() (FileWatcher, error) { 19 if watcher, err := NewEventWatcher(); err == nil { 20 return watcher, nil 21 } 22 return NewPollingWatcher(), nil 23 } 24 25 // NewPollingWatcher returns a poll-based file watcher 26 func NewPollingWatcher() FileWatcher { 27 return &filePoller{ 28 events: make(chan fsnotify.Event), 29 errors: make(chan error), 30 } 31 } 32 33 // NewEventWatcher returns an fs-event based file watcher 34 func NewEventWatcher() (FileWatcher, error) { 35 watcher, err := fsnotify.NewWatcher() 36 if err != nil { 37 return nil, err 38 } 39 return &fsNotifyWatcher{watcher}, nil 40 }