Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding Unwatch function for file provider #245

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 15 additions & 5 deletions providers/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
// File implements a File provider.
type File struct {
path string
w *fsnotify.Watcher
}

// Provider returns a file provider.
Expand Down Expand Up @@ -48,7 +49,7 @@ func (f *File) Watch(cb func(event interface{}, err error)) error {
// the whole parent directory to pick up all events such as symlink changes.
fDir, _ := filepath.Split(f.path)

w, err := fsnotify.NewWatcher()
f.w, err = fsnotify.NewWatcher()
rhnvrm marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
Expand All @@ -62,7 +63,7 @@ func (f *File) Watch(cb func(event interface{}, err error)) error {
loop:
for {
select {
case event, ok := <-w.Events:
case event, ok := <-f.w.Events:
if !ok {
cb(nil, errors.New("fsnotify watch channel closed"))
break loop
Expand Down Expand Up @@ -108,7 +109,7 @@ func (f *File) Watch(cb func(event interface{}, err error)) error {
cb(nil, nil)

// There's an error.
case err, ok := <-w.Errors:
case err, ok := <-f.w.Errors:
if !ok {
cb(nil, errors.New("fsnotify err channel closed"))
break loop
Expand All @@ -120,9 +121,18 @@ func (f *File) Watch(cb func(event interface{}, err error)) error {
}
}

w.Close()
f.w.Close()
}()

// Watch the directory for changes.
return w.Add(fDir)
return f.w.Add(fDir)
}

func (f *File) UnWatch() error {
prateek-narsinghani marked this conversation as resolved.
Show resolved Hide resolved
fDir, _ := filepath.Split(f.path)
err := f.w.Remove(fDir)
prateek-narsinghani marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
return nil
}