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

Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
adding Unwatch function for file provider
prateeknarsinghani committed Oct 31, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 313886424a5e956f8cc015a9bcdd6f5ad703a408
20 changes: 15 additions & 5 deletions providers/file/file.go
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ import (
// File implements a File provider.
type File struct {
path string
w *fsnotify.Watcher
}

// Provider returns a file provider.
@@ -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
}
@@ -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
@@ -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
@@ -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
}