2016-02-24 05:05:16 +03:00
|
|
|
// Package filenotify provides a mechanism for watching file(s) for changes.
|
|
|
|
// Generally leans on fsnotify, but provides a poll-based notifier which fsnotify does not support.
|
|
|
|
// These are wrapped up in a common interface so that either can be used interchangeably in your code.
|
|
|
|
package filenotify
|
|
|
|
|
2016-10-31 21:22:28 +03:00
|
|
|
import "github.com/fsnotify/fsnotify"
|
2016-02-24 05:05:16 +03:00
|
|
|
|
|
|
|
// FileWatcher is an interface for implementing file notification watchers
|
|
|
|
type FileWatcher interface {
|
|
|
|
Events() <-chan fsnotify.Event
|
|
|
|
Errors() <-chan error
|
|
|
|
Add(name string) error
|
|
|
|
Remove(name string) error
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// New tries to use an fs-event watcher, and falls back to the poller if there is an error
|
|
|
|
func New() (FileWatcher, error) {
|
|
|
|
if watcher, err := NewEventWatcher(); err == nil {
|
|
|
|
return watcher, nil
|
|
|
|
}
|
|
|
|
return NewPollingWatcher(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPollingWatcher returns a poll-based file watcher
|
|
|
|
func NewPollingWatcher() FileWatcher {
|
|
|
|
return &filePoller{
|
|
|
|
events: make(chan fsnotify.Event),
|
|
|
|
errors: make(chan error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEventWatcher returns an fs-event based file watcher
|
|
|
|
func NewEventWatcher() (FileWatcher, error) {
|
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &fsNotifyWatcher{watcher}, nil
|
|
|
|
}
|