diff --git a/README.md b/README.md index f1dc9b5..663332c 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/main.go b/main.go index fdde3db..e7f0c9b 100644 --- a/main.go +++ b/main.go @@ -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", diff --git a/watcher.go b/watcher.go index 46e2334..9dfc315 100644 --- a/watcher.go +++ b/watcher.go @@ -40,36 +40,56 @@ 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 { - if path == ".git" && info.IsDir() { - return filepath.SkipDir + for i := range w.WatchItems { + fileChanged, err := w.scanChange(w.WatchItems[i]) + if err != nil { + w.Errors <- err + return } - for _, x := range w.IgnoreItems { - if x == path { - return filepath.SkipDir - } - } - - // ignore hidden files - 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 + if fileChanged != "" { + w.Events <- fileChanged startTime = time.Now() - return errDetectedChange } - - return nil - }) - - if err != nil && err != errDetectedChange { - w.Errors <- err } + 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 + } + + for _, x := range w.IgnoreItems { + if x == path { + return filepath.SkipDir + } + } + + // ignore hidden files + if filepath.Base(path)[0] == '.' { + return nil + } + + ext := filepath.Ext(path) + if _, ok := w.AllowedExtensions[ext]; ok && info.ModTime().After(startTime) { + fileChanged = path + return errDetectedChange + } + + return nil + }) + + if err != nil && err != errDetectedChange { + return "", err + } + + return fileChanged, nil +}