Skip to content

Commit

Permalink
update docs and address cyclomatic complexity of watcher.Watch
Browse files Browse the repository at this point in the history
  • Loading branch information
wilhelm-murdoch committed Jun 2, 2022
1 parent dea00c1 commit 18c057d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 22 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.

Expand Down
66 changes: 50 additions & 16 deletions watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand All @@ -148,7 +183,6 @@ func (w *Watcher) Watch() error {
if err != nil {
return err
}

case <-w.done:
w.fsnotify.Close()
close(w.done)
Expand Down

0 comments on commit 18c057d

Please sign in to comment.