bcnotify

package module
v0.0.0-...-07239d1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 2, 2015 License: MIT Imports: 6 Imported by: 0

README

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.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrWatcherClosed = fmt.Errorf("FileSystemWatcher closed")

ErrWatcherClosed is returned to allow for clean shutting down of a watcher.

Functions

This section is empty.

Types

type Event

type Event struct {
	Name string // Relative path to the file or directory.
	Op   Op     // File operation that triggered the event.
	// contains filtered or unexported fields
}

Event represents a single file system notification.

func (Event) String

func (e Event) String() string

type FileSystemWatcher

type FileSystemWatcher struct {
	// contains filtered or unexported fields
}

FileSystemWatcher represents a structure used to watch files on the file system.

func NewFileSystemWatcher

func NewFileSystemWatcher() (*FileSystemWatcher, error)

NewFileSystemWatcher returns an initialized *FileSystemWatcher.

func (*FileSystemWatcher) AddDir

func (fw *FileSystemWatcher) AddDir(path, pattern string, ops Op, recursive bool) error

AddDir adds a directory to be watched, returning an error if any. It allows a filter to be specified on which files to watch. It also allows recursive watching.

func (*FileSystemWatcher) AddFile

func (fw *FileSystemWatcher) AddFile(path string, ops Op) error

AddFile adds a file to be watched along with an Op on which to filter events, // returning an error if any.

func (*FileSystemWatcher) Close

func (fw *FileSystemWatcher) Close() error

Close closes the system resources for this FileSystemWatcher

func (*FileSystemWatcher) NotifyEvent

func (fw *FileSystemWatcher) NotifyEvent(notify func(*Event, error))

NotifyEvent accepts a function that takes a *bcnotify.Event and error and calls that function whenever an event or error happens.

func (*FileSystemWatcher) RemoveDir

func (fw *FileSystemWatcher) RemoveDir(path string, recursive bool) error

RemoveDir removes a directory from being watched, returning an error if any. It also allows recursive removal.

func (*FileSystemWatcher) RemoveFile

func (fw *FileSystemWatcher) RemoveFile(path string) error

RemoveFile removes a file from being watched and returns and error if any.

func (*FileSystemWatcher) WaitEvent

func (fw *FileSystemWatcher) WaitEvent() (*Event, error)

WaitEvent blocks and waits until an event or error comes through. This needs to be called in a go routine, probably in a loop.

type Op

type Op uint32

Op describes a set of file operations.

const (
	Create Op = 1 << iota
	Write
	Remove
	Rename
	Chmod

	AllOps = Create | Write | Remove | Rename | Chmod
)

These are the generalized file operations that can trigger a notification.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL