From ebdd7f47d353306a461738ed0465ad36d9d541f4 Mon Sep 17 00:00:00 2001 From: Toby Wilkes Date: Thu, 8 Aug 2019 00:00:17 +0900 Subject: [PATCH] Add watch-ignore command --- common.go | 2 +- watch.go | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/common.go b/common.go index 9a601b3..5d300d1 100644 --- a/common.go +++ b/common.go @@ -14,7 +14,7 @@ import ( // Binary name used for built package const binaryName = "watcher" -var watcherFlags = []string{"run", "watch", "watch-vendor"} +var watcherFlags = []string{"run", "watch", "watch-vendor", "watch-ignore"} // Params is used for keeping go-watcher and application flag parameters type Params struct { diff --git a/watch.go b/watch.go index 0704936..1674e36 100644 --- a/watch.go +++ b/watch.go @@ -28,6 +28,7 @@ type Watcher struct { rootdir string watcher *fsnotify.Watcher watchVendor bool + ignorePaths string // when a file gets changed a message is sent to the update channel update chan struct{} } @@ -49,6 +50,7 @@ func MustRegisterWatcher(params *Params) *Watcher { update: make(chan struct{}), rootdir: params.Get("watch"), watchVendor: watchVendor, + ignorePaths: params.Get("watch-ignore"), } w.watcher, err = fsnotify.NewWatcher() @@ -130,6 +132,9 @@ func (w *Watcher) watchFolders() { log.Fatalf("Could not get root working directory: %s", err) } + // Unpack ignores + ignorePaths := strings.Split(w.ignorePaths, ";") + filepath.Walk(wd, func(path string, info os.FileInfo, err error) error { // skip files if info == nil { @@ -148,6 +153,14 @@ func (w *Watcher) watchFolders() { } } + // skip ignored files + relative := strings.TrimPrefix(path, wd) + for _, ignorePath := range ignorePaths { + if strings.TrimPrefix(relative, "/") == ignorePath { + return filepath.SkipDir + } + } + // skip hidden folders if len(path) > 1 && strings.HasPrefix(filepath.Base(path), ".") { return filepath.SkipDir