-
Notifications
You must be signed in to change notification settings - Fork 159
Add polling-style file watch option #53
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3fc8055
add file watcher using polling method
HurSungYun 254db83
change module name
HurSungYun 4b0afac
fix: use filepath.Walk to filter exclude-dir properly
HurSungYun 8793058
chore: refactor common logics
HurSungYun 68bbabc
change module name
HurSungYun 18e1b10
Apply suggestions from code review
HurSungYun 97ffc98
refactor global variables
HurSungYun fb7fce4
make polling interval a parameter
HurSungYun 36b48cd
update README.md
HurSungYun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/fsnotify/fsnotify" | ||
pollingWatcher "github.com/radovskyb/watcher" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"syscall" | ||
"time" | ||
) | ||
|
||
type FileWatcher interface { | ||
Close() error | ||
AddFiles(pattern *regexp.Regexp) error | ||
add(path string) error | ||
Watch(jobs chan<- string, pattern *regexp.Regexp) | ||
} | ||
|
||
type NotifyWatcher struct { | ||
watcher *fsnotify.Watcher | ||
} | ||
|
||
func (n NotifyWatcher) Close() error { | ||
return n.watcher.Close() | ||
} | ||
|
||
func (n NotifyWatcher) AddFiles(pattern *regexp.Regexp) error { | ||
return addFiles(n) | ||
} | ||
|
||
func (n NotifyWatcher) Watch(jobs chan<- string, pattern *regexp.Regexp) { | ||
for { | ||
select { | ||
case ev := <-n.watcher.Events: | ||
if ev.Op&fsnotify.Remove == fsnotify.Remove || ev.Op&fsnotify.Write == fsnotify.Write || ev.Op&fsnotify.Create == fsnotify.Create { | ||
base := filepath.Base(ev.Name) | ||
|
||
// Assume it is a directory and track it. | ||
if *flagRecursive == true && !flagExcludedDirs.Matches(ev.Name) { | ||
HurSungYun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
n.watcher.Add(ev.Name) | ||
} | ||
|
||
if flagIncludedFiles.Matches(base) || matchesPattern(pattern, ev.Name) { | ||
if !flagExcludedFiles.Matches(base) { | ||
HurSungYun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
jobs <- ev.Name | ||
} | ||
} | ||
} | ||
|
||
case err := <-n.watcher.Errors: | ||
if v, ok := err.(*os.SyscallError); ok { | ||
if v.Err == syscall.EINTR { | ||
continue | ||
} | ||
log.Fatal("watcher.Error: SyscallError:", v) | ||
} | ||
log.Fatal("watcher.Error:", err) | ||
} | ||
} | ||
} | ||
|
||
func (n NotifyWatcher) add(path string) error { | ||
return n.watcher.Add(path) | ||
} | ||
|
||
type PollingWatcher struct { | ||
watcher *pollingWatcher.Watcher | ||
} | ||
|
||
func (p PollingWatcher) Close() error { | ||
p.watcher.Close() | ||
return nil | ||
} | ||
|
||
func (p PollingWatcher) AddFiles(pattern *regexp.Regexp) error { | ||
p.watcher.AddFilterHook(pollingWatcher.RegexFilterHook(pattern, false)) | ||
|
||
return addFiles(p) | ||
} | ||
|
||
func (p PollingWatcher) Watch(jobs chan<- string, pattern *regexp.Regexp) { | ||
// Start the watching process. | ||
go func() { | ||
if err := p.watcher.Start(PollingInterval * time.Millisecond); err != nil { | ||
log.Fatalln(err) | ||
} | ||
}() | ||
|
||
for { | ||
select { | ||
case event := <-p.watcher.Event: | ||
if *flagVerbose { | ||
// Print the event's info. | ||
fmt.Println(event) | ||
} | ||
|
||
base := filepath.Base(event.Path) | ||
|
||
if flagIncludedFiles.Matches(base) || matchesPattern(pattern, event.Path) { | ||
if !flagExcludedFiles.Matches(base) { | ||
jobs <- event.String() | ||
} | ||
} | ||
case err := <-p.watcher.Error: | ||
if err == pollingWatcher.ErrWatchedFileDeleted { | ||
fmt.Println(err) | ||
HurSungYun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
continue | ||
} | ||
log.Fatalln(err) | ||
case <-p.watcher.Closed: | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (p PollingWatcher) add(path string) error { | ||
return p.watcher.Add(path) | ||
} | ||
|
||
func NewWatcher(usePolling bool) (FileWatcher, error) { | ||
if usePolling { | ||
w := pollingWatcher.New() | ||
return PollingWatcher{ | ||
watcher: w, | ||
}, nil | ||
} else { | ||
w, err := fsnotify.NewWatcher() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return NotifyWatcher{ | ||
watcher: w, | ||
}, nil | ||
} | ||
} | ||
|
||
func addFiles(fw FileWatcher) error { | ||
for _, flagDirectory := range flagDirectories { | ||
if *flagRecursive == true { | ||
err := filepath.Walk(flagDirectory, func(path string, info os.FileInfo, err error) error { | ||
if err == nil && info.IsDir() { | ||
if flagExcludedDirs.Matches(path) { | ||
return filepath.SkipDir | ||
} else { | ||
if *flagVerbose { | ||
log.Printf("Watching directory '%s' for changes.\n", path) | ||
} | ||
return fw.add(path) | ||
} | ||
} | ||
return err | ||
}) | ||
|
||
if err != nil { | ||
return fmt.Errorf("filepath.Walk(): %v", err) | ||
} | ||
|
||
if err := fw.add(flagDirectory); err != nil { | ||
return fmt.Errorf("FileWatcher.Add(): %v", err) | ||
} | ||
} else { | ||
if err := fw.add(flagDirectory); err != nil { | ||
return fmt.Errorf("FileWatcher.AddFiles(): %v", err) | ||
} | ||
} | ||
} | ||
return nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.