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

Add watch-ignore command #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}
Expand All @@ -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()
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down