bcnotify
is a layer on top of fsnotify.v1 to make it easier to work with. Includes recursive adding of directories and filtering events.
No. It has not yet been used in production and tests sometimes pass, sometimes fail for reasons I don't understand, although I believe the problem is with the test code, not the package code.
bcnotify
monitors file system events. You begin by calling NewFileSystemWatcher()
to get a FileSystemWatcher
. You will want to make sure you call the Close
method on that watcher to clean up when you are finished with it.
fw, err := bcnotify.NewFileSystemWatcher()
// Error handling...
defer fw.Close()
To watch a specific file for events, use the AddFile
method. You can specify the Op (operations) you want to monitor.
err := fw.AddFile(filename, bcnotify.AllOps)
which is the same as
err := fw.AddFile(filename, bcnotify.Create|bcnotify.Write|bcnotify.Chmod|bcnotify.Rename|bcnotify.Remove)
To monitor a directory for file events, use the AddDir
method. You can add a directory recursively or not.
Call with:
- Path to the directory to monitor.
- File path filter to only monitor certain files in the directory. Use "" for no filtering.
- File operations to monitor.
- Whether to use recursion to add subdirectories.
// Only monitor files that have the ".txt" extension,
// only the Create operation on files,
// and use recursion (add subdirectories).
err := fw.AddDir(dir, "*.txt", bcnotify.Create, true)
File path filters use the filepath.Match
method for matching. You can see the documentation for it here. Matching is performed only on the filename, the directory is not considered.
When you have added the files or directories you want to monitor, you then need to get the events. There are two methods for this.
WaitEvent
is a blocking method that waits until a filesystem event is fired. Unless you only want to receive one event, you will want to put it in a goroutine and within a loop.
go func(){
for {
event, err := fw.WaitEvent()
// Error handling...
// Event handling
fmt.Println(event.Name)
}
}()
The bcnotify.Event
that is returned is API compatible with fsnotify.Event
.
NotifyEvent
allows registering a function to receive all filesystem events.
fw.NotifyEvent(func(event *bcnotify.Event, err error) {
// Error handling...
if event.Op&bcnotify.Create == bcnotify.Create {
// Handle create operation
}
})
"BC" are the initials of my fiancé. I couldn't think of anything else to call it.