Add support for glob searches on watch and ignore options

This commit is contained in:
Max Claus Nunes 2018-06-18 23:47:20 -03:00
parent 6fa6a9110c
commit 853e82c957
5 changed files with 59 additions and 7 deletions

11
Gopkg.lock generated
View File

@ -25,6 +25,15 @@
revision = "02e3cf038dcea8290e44424da473dd12be796a8a" revision = "02e3cf038dcea8290e44424da473dd12be796a8a"
version = "v1.0.3" version = "v1.0.3"
[[projects]]
branch = "master"
name = "github.com/mattn/go-zglob"
packages = [
".",
"fastwalk"
]
revision = "49693fbb3fe3c3a75fc4e4d6fb1d7cedcbdeb385"
[[projects]] [[projects]]
name = "github.com/urfave/cli" name = "github.com/urfave/cli"
packages = ["."] packages = ["."]
@ -40,6 +49,6 @@
[solve-meta] [solve-meta]
analyzer-name = "dep" analyzer-name = "dep"
analyzer-version = 1 analyzer-version = 1
inputs-digest = "bd09f9274a4112aeb869a026bcf3ab61443289a245d3d353da6623f3327e7b4e" inputs-digest = "01046dcc133438d0c6e244499b12395a2ef02264beec4752652b8c78c571a08e"
solver-name = "gps-cdcl" solver-name = "gps-cdcl"
solver-version = 1 solver-version = 1

View File

@ -13,3 +13,7 @@
[prune] [prune]
go-tests = true go-tests = true
unused-packages = true unused-packages = true
[[constraint]]
branch = "master"
name = "github.com/mattn/go-zglob"

View File

@ -40,6 +40,13 @@ func (l *Logger) Debug(v ...interface{}) {
} }
} }
// Debugf ...
func (l *Logger) Debugf(format string, v ...interface{}) {
if l.verbose {
l.logDebug.Printf(format, v...)
}
}
// Warn ... // Warn ...
func (l *Logger) Warn(v ...interface{}) { func (l *Logger) Warn(v ...interface{}) {
l.logWarn.Println(v...) l.logWarn.Println(v...)

View File

@ -162,7 +162,10 @@ func runGaper(cfg *Config) error {
return fmt.Errorf("run error: %v", err) return fmt.Errorf("run error: %v", err)
} }
watcher := NewWatcher(cfg.PollInterval, cfg.WatchItems, cfg.IgnoreItems, cfg.Extensions) watcher, err := NewWatcher(cfg.PollInterval, cfg.WatchItems, cfg.IgnoreItems, cfg.Extensions)
if err != nil {
return fmt.Errorf("watcher error: %v", err)
}
go watcher.Watch() go watcher.Watch()
for { for {

View File

@ -2,9 +2,12 @@ package main
import ( import (
"errors" "errors"
"fmt"
"os" "os"
"path/filepath" "path/filepath"
"time" "time"
zglob "github.com/mattn/go-zglob"
) )
// Watcher ... // Watcher ...
@ -18,27 +21,37 @@ type Watcher struct {
} }
// NewWatcher ... // NewWatcher ...
func NewWatcher(pollInterval int, watchItems []string, ignoreItems []string, extensions []string) *Watcher { func NewWatcher(pollInterval int, watchItems []string, ignoreItems []string, extensions []string) (*Watcher, error) {
allowedExts := make(map[string]bool) allowedExts := make(map[string]bool)
for _, ext := range extensions { for _, ext := range extensions {
allowedExts["."+ext] = true allowedExts["."+ext] = true
} }
watchMatches, err := resolveGlobMatches(watchItems)
if err != nil {
return nil, err
}
ignoreMatches, err := resolveGlobMatches(ignoreItems)
if err != nil {
return nil, err
}
return &Watcher{ return &Watcher{
Events: make(chan string), Events: make(chan string),
Errors: make(chan error), Errors: make(chan error),
PollInterval: pollInterval, PollInterval: pollInterval,
WatchItems: watchItems, WatchItems: watchMatches,
IgnoreItems: ignoreItems, IgnoreItems: ignoreMatches,
AllowedExtensions: allowedExts, AllowedExtensions: allowedExts,
} }, nil
} }
var startTime = time.Now() var startTime = time.Now()
var errDetectedChange = errors.New("done") var errDetectedChange = errors.New("done")
// Watch ... // Watch ...
func (w *Watcher) Watch() { // nolint: gocyclo func (w *Watcher) Watch() {
for { for {
for i := range w.WatchItems { for i := range w.WatchItems {
fileChanged, err := w.scanChange(w.WatchItems[i]) fileChanged, err := w.scanChange(w.WatchItems[i])
@ -93,3 +106,19 @@ func (w *Watcher) scanChange(watchPath string) (string, error) {
return fileChanged, nil return fileChanged, nil
} }
func resolveGlobMatches(paths []string) ([]string, error) {
var result []string
for _, path := range paths {
matches, err := zglob.Glob(path)
if err != nil {
return nil, fmt.Errorf("couldn't resolve glob path %s: %v", path, err)
}
logger.Debugf("Resolved glob path %s: %v", path, matches)
result = append(result, matches...)
}
return result, nil
}