Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
/ bcnotify Public archive

Layer on top of fsnotify to make it easier to work with. Includes recursive adding of directories and filtering events.

License

Notifications You must be signed in to change notification settings

flowonyx/bcnotify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Coverage GoDoc

Layer on top of fsnotify

bcnotify is a layer on top of fsnotify.v1 to make it easier to work with. Includes recursive adding of directories and filtering events.

Is it production ready?

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.

How do I use it?

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)
Filtering

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

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

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
  }
})

Why the Name?

"BC" are the initials of my fiancé. I couldn't think of anything else to call it.

About

Layer on top of fsnotify to make it easier to work with. Includes recursive adding of directories and filtering events.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages