Simple Golang inotify wrapper.
-
Low level
Inotify- wrapper around inotify(7)InotifyEvent- generated file/folder event. ContainsName(full path), watch descriptior andMaskthat describes the event.
-
Higher level
FileWatcher- higher level utility, helps to watch the list of files for changes, creation or removalDirWatcher- higher level utility, recursively watches given root folder for added, removed or changed files.FileEvent- embedsInotifyEventand keeps additional fieldEofto notify user that there will be no more events.
Use FileWatcher and DirWatcher as an example and build your own utility classes.
package main
import (
"fmt"
"github.com/illarion/gonotify/v2"
"time"
"context"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
watcher, err := gonotify.NewDirWatcher(ctx, gonotify.IN_CREATE|gonotify.IN_CLOSE, "/tmp")
if err != nil {
panic(err)
}
for {
select {
case event := <-watcher.C:
fmt.Printf("Event: %s\n", event)
if event.Mask&gonotify.IN_CREATE != 0 {
fmt.Printf("File created: %s\n", event.Name)
}
if event.Mask&gonotify.IN_CLOSE != 0 {
fmt.Printf("File closed: %s\n", event.Name)
}
case <-time.After(5 * time.Second):
fmt.Println("Timeout")
cancel()
return
}
}
}MIT. See LICENSE file for more details.