This is similar to Add(), except that you can pass options. Ideally this
should just be Add(), but that's not a compatible change, so we're stuck
with this until we do a v2.
There are quite a few enhancements that depend on *some* way to pass
options; as an example I added WithBufferSize() to control the buffer
size for (see #72) for the Windows backend, because that one is fairly
trivial to implement:
w, err := fsnotify.NewWatcher()
err = w.AddWith("/path", fsnotify.WithBufferSize(65536*4))
Some other options we might want to add:
err = w.AddWith("/path",
fsnotify.WithEvents(fsnotify.Open | fsnotify.Close), // Filter events
fsnotify.WithPoll(time.Second), // Use poll watcher
fsnotify.WithFanotify(), // Prefer fanotify on Linux
fsnotify.WithFollowLinks(true), // Control symlink follow behaviour
fsnotify.WithDebounce(100*time.Milliseconds), // Automatically debounce duplicate events
fsnotify.WithRetry(1*time.Second, 1*time.Minute), // Retry every second if the path disappears for a minute
)
These are just some ideas, nothing fixed here yet. Some of these options
are likely to change once I get around to actually working on it.
This uses "functional options" so we can add more later. Options are
passed to Add() rather than the Watcher itself, so the behaviour can be
modified for every watch, rather than being global. This way you can do
things like watch /nfs-drive with a poll backend, and use the regular OS
backend for ~/dir, without having to create two watchers.
This upgrades fairly nicely to v2 where we rename AddWith() to Add():
err = w.Add("/path",
fsnotify.WithBufferSize(65536*4),
fsnotify.WithEvents(fsnotify.Open | fsnotify.Close))
Folks will just have to s/fsnotify.AddWith/fsnotify.Add/, without having
to change all the option names. Plus having a consistent prefix
autocompletes nicely in editors.
Fixes #72