Add support to watch multiple paths

This commit is contained in:
Max Claus Nunes 2018-06-18 23:22:18 -03:00
parent 28cf3176d3
commit 6fa6a9110c
3 changed files with 49 additions and 29 deletions

View File

@ -41,8 +41,8 @@ GLOBAL OPTIONS:
--build-path value path to the program source code
--build-args value build arguments passed to the program
--verbose turns on the verbose messages from Gaper
--watch value, -w value a comma-delimited list of folders or files to watch for changes
--ignore value, -i value a comma-delimited list of folders or files to ignore for changes
--watch value, -w value list of folders or files to watch for changes
--ignore value, -i value list of folders or files to ignore for changes
--poll-interval value, -p value how often in milliseconds to poll watched files for changes (default: 500)
--extensions value, -e value a comma-delimited list of file extensions to watch for changes (default: "go")
----no-restart-on value, -n value don't automatically restart the executed program if it ends.

View File

@ -80,11 +80,11 @@ func main() {
},
cli.StringSliceFlag{
Name: "watch, w",
Usage: "a comma-delimited list of folders or files to watch for changes",
Usage: "list of folders or files to watch for changes",
},
cli.StringSliceFlag{
Name: "ignore, i",
Usage: "a comma-delimited list of folders or files to ignore for changes",
Usage: "list of folders or files to ignore for changes",
},
cli.IntFlag{
Name: "poll-interval, p",

View File

@ -40,7 +40,29 @@ var errDetectedChange = errors.New("done")
// Watch ...
func (w *Watcher) Watch() { // nolint: gocyclo
for {
err := filepath.Walk(w.WatchItems[0], func(path string, info os.FileInfo, err error) error {
for i := range w.WatchItems {
fileChanged, err := w.scanChange(w.WatchItems[i])
if err != nil {
w.Errors <- err
return
}
if fileChanged != "" {
w.Events <- fileChanged
startTime = time.Now()
}
}
time.Sleep(time.Duration(w.PollInterval) * time.Millisecond)
}
}
func (w *Watcher) scanChange(watchPath string) (string, error) {
logger.Debug("Watching ", watchPath)
var fileChanged string
err := filepath.Walk(watchPath, func(path string, info os.FileInfo, err error) error {
if path == ".git" && info.IsDir() {
return filepath.SkipDir
}
@ -55,12 +77,10 @@ func (w *Watcher) Watch() { // nolint: gocyclo
if filepath.Base(path)[0] == '.' {
return nil
}
time.Sleep(time.Duration(w.PollInterval) * time.Millisecond)
ext := filepath.Ext(path)
if _, ok := w.AllowedExtensions[ext]; ok && info.ModTime().After(startTime) {
w.Events <- path
startTime = time.Now()
fileChanged = path
return errDetectedChange
}
@ -68,8 +88,8 @@ func (w *Watcher) Watch() { // nolint: gocyclo
})
if err != nil && err != errDetectedChange {
w.Errors <- err
return "", err
}
}
return fileChanged, nil
}