From 18c057d45f8ab7a25ee23211285c02a84a183980 Mon Sep 17 00:00:00 2001 From: Wilhelm Murdoch Date: Fri, 3 Jun 2022 01:03:55 +1000 Subject: [PATCH] update docs and address cyclomatic complexity of watcher.Watch --- README.md | 12 +++++----- watcher.go | 66 +++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 56 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 5d95066..aebe9bb 100644 --- a/README.md +++ b/README.md @@ -62,8 +62,8 @@ On fires off an assigned callback for each event type. Only specified events are All will fire off the specified callback on any supported `fsnotify` event. ### Function `Watch` -* `func (w *Watcher) Watch() error` [#](watcher.go#L105) -* `watcher.go:105:161` [#](watcher.go#L105-L161) +* `func (w *Watcher) Watch() error` [#](watcher.go#L150) +* `watcher.go:150:195` [#](watcher.go#L150-L195) Watch creates a new `errgroup` instance and monitors for changes to any of the specified files. All supported event types will fire off specified callbacks if available. This method exits on the first encountered error. @@ -83,14 +83,14 @@ func main() { ``` ### Function `List` -* `func (w *Watcher) List() []string` [#](watcher.go#L166) -* `watcher.go:166:168` [#](watcher.go#L166-L168) +* `func (w *Watcher) List() []string` [#](watcher.go#L200) +* `watcher.go:200:202` [#](watcher.go#L200-L202) List is a wrapper around `fsnotify.Watchlist()`. It returns a list of strings representing all files and directories currently monitored instance of `fsnotify`. ### Function `Done` -* `func (w *Watcher) Done()` [#](watcher.go#L172) -* `watcher.go:172:174` [#](watcher.go#L172-L174) +* `func (w *Watcher) Done()` [#](watcher.go#L206) +* `watcher.go:206:208` [#](watcher.go#L206-L208) Done signals a blocking channel that processing is complete and that we can safely exit the current watcher instance. diff --git a/watcher.go b/watcher.go index e800e9a..9c6803d 100644 --- a/watcher.go +++ b/watcher.go @@ -99,6 +99,51 @@ func (w *Watcher) All(f func(fsnotify.Event, os.FileInfo, error) error) { w.hasCallbacks = true } +// getWriteCallbackOrNil is a convenience method used to access the relevant callback. +func (w *Watcher) getWriteCallbackOrNil(event fsnotify.Event, info os.FileInfo, err error) error { + if w.onWrite != nil { + return w.onWrite(event, info, err) + } + + return nil +} + +// getCreateCallbackOrNil is a convenience method used to access the relevant callback. +func (w *Watcher) getCreateCallbackOrNil(event fsnotify.Event, info os.FileInfo, err error) error { + if w.onCreate != nil { + return w.onCreate(event, info, err) + } + + return nil +} + +// getRemoveCallbackOrNil is a convenience method used to access the relevant callback. +func (w *Watcher) getRemoveCallbackOrNil(event fsnotify.Event, info os.FileInfo, err error) error { + if w.onRemove != nil { + return w.onRemove(event, info, err) + } + + return nil +} + +// getRenameCallbackOrNil is a convenience method used to access the relevant callback. +func (w *Watcher) getRenameCallbackOrNil(event fsnotify.Event, info os.FileInfo, err error) error { + if w.onRename != nil { + return w.onRename(event, info, err) + } + + return nil +} + +// getChmodCallbackOrNil is a convenience method used to access the relevant callback. +func (w *Watcher) getChmodCallbackOrNil(event fsnotify.Event, info os.FileInfo, err error) error { + if w.onChmod != nil { + return w.onChmod(event, info, err) + } + + return nil +} + // Watch creates a new `errgroup` instance and monitors for changes to any of // the specified files. All supported event types will fire off specified // callbacks if available. This method exits on the first encountered error. @@ -120,25 +165,15 @@ func (w *Watcher) Watch() error { info, err := os.Stat(event.Name) switch { case event.Op&fsnotify.Write == fsnotify.Write: - if w.onWrite != nil { - err = w.onWrite(event, info, err) - } + err = w.getWriteCallbackOrNil(event, info, err) case event.Op&fsnotify.Create == fsnotify.Create: - if w.onCreate != nil { - err = w.onCreate(event, info, err) - } + err = w.getCreateCallbackOrNil(event, info, err) case event.Op&fsnotify.Remove == fsnotify.Remove: - if w.onRemove != nil { - err = w.onRemove(event, info, err) - } + err = w.getRemoveCallbackOrNil(event, info, err) case event.Op&fsnotify.Rename == fsnotify.Rename: - if w.onRename != nil { - err = w.onRename(event, info, err) - } + err = w.getRenameCallbackOrNil(event, info, err) case event.Op&fsnotify.Chmod == fsnotify.Chmod: - if w.onChmod != nil { - err = w.onChmod(event, info, err) - } + err = w.getChmodCallbackOrNil(event, info, err) } if w.onAll != nil { @@ -148,7 +183,6 @@ func (w *Watcher) Watch() error { if err != nil { return err } - case <-w.done: w.fsnotify.Close() close(w.done)