Documentation ¶
Overview ¶
Example ¶
dir, err := os.MkdirTemp("", "example") if err != nil { log.Fatal(err) } defer os.RemoveAll(dir) notifier, err := filewatcher.NewFSNotify() if err != nil { log.Fatal(err) } watcher := filewatcher.New(notifier, pubsub.NewInprocess()) dstFile := filepath.Join(dir, "fsnotify.txt") go func() { // Test scenario. sleep := func() { time.Sleep(1 * time.Second) } sleep() // No events. if err := os.WriteFile(dstFile, []byte("hello"), 0666); err != nil { log.Fatal(err) } sleep() // 2 events: CREATE + WRITE if err := os.Remove(dstFile); err != nil { log.Fatal(err) } sleep() // 1 event: REMOVE watcher.Close() }() it := watcher.MustWatch(dstFile) defer it.Close() for it.Next(context.Background()) { name := filepath.Base(it.Text()) fmt.Printf("NAME: %q; ERROR: %v\n", name, it.Err()) if errors.Is(it.Err(), context.DeadlineExceeded) { break } else if it.Err() != nil { continue } if b, err := os.ReadFile(it.Text()); os.IsNotExist(err) { fmt.Println("NO FILE") } else if err != nil { fmt.Println("OTHER ERROR") } else { fmt.Printf("CONTENT: %q\n", b) } }
Output: NAME: "fsnotify.txt"; ERROR: <nil> CONTENT: "hello" NAME: "fsnotify.txt"; ERROR: <nil> CONTENT: "hello" NAME: "fsnotify.txt"; ERROR: <nil> NO FILE
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrClosed = errors.New("file watcher is closed")
Functions ¶
This section is empty.
Types ¶
type FSNotify ¶
type FSNotify struct {
// contains filtered or unexported fields
}
FSNotify implements fsnotify watcher.
func NewFSNotify ¶
type FileWatcher ¶
type FileWatcher struct {
// contains filtered or unexported fields
}
FileWatcher listens to file changes. Close method should be called to free allocated resources.
func New ¶
func New(watcher WatchCloser, be pubsub.Backend) FileWatcher
New returns new file watcher ready to observe file changes. Close method should be called when this instance isn't needed anymore to free resources.
func (FileWatcher) Close ¶
func (fw FileWatcher) Close() error
Close stops the watcher and frees allocated resources.
func (FileWatcher) MustWatch ¶
func (fw FileWatcher) MustWatch(name string) *Iterator
MustWatch is a version of Watch which panics if there was an error.
type Iterator ¶
type Iterator struct {
// contains filtered or unexported fields
}
Iterators iterates every time there is a file change.
func (*Iterator) Err ¶
Err returns an error received by the last Next() call. Note that stream termination error is ignored and Next() always returns false afterwards.
Click to show internal directories.
Click to hide internal directories.