Skip to content

Commit 853e82c

Browse files
committed
Add support for glob searches on watch and ignore options
1 parent 6fa6a91 commit 853e82c

File tree

5 files changed

+59
-7
lines changed

5 files changed

+59
-7
lines changed

Gopkg.lock

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@
1313
[prune]
1414
go-tests = true
1515
unused-packages = true
16+
17+
[[constraint]]
18+
branch = "master"
19+
name = "github.com/mattn/go-zglob"

loggger.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ func (l *Logger) Debug(v ...interface{}) {
4040
}
4141
}
4242

43+
// Debugf ...
44+
func (l *Logger) Debugf(format string, v ...interface{}) {
45+
if l.verbose {
46+
l.logDebug.Printf(format, v...)
47+
}
48+
}
49+
4350
// Warn ...
4451
func (l *Logger) Warn(v ...interface{}) {
4552
l.logWarn.Println(v...)

main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,10 @@ func runGaper(cfg *Config) error {
162162
return fmt.Errorf("run error: %v", err)
163163
}
164164

165-
watcher := NewWatcher(cfg.PollInterval, cfg.WatchItems, cfg.IgnoreItems, cfg.Extensions)
165+
watcher, err := NewWatcher(cfg.PollInterval, cfg.WatchItems, cfg.IgnoreItems, cfg.Extensions)
166+
if err != nil {
167+
return fmt.Errorf("watcher error: %v", err)
168+
}
166169

167170
go watcher.Watch()
168171
for {

watcher.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ package main
22

33
import (
44
"errors"
5+
"fmt"
56
"os"
67
"path/filepath"
78
"time"
9+
10+
zglob "github.com/mattn/go-zglob"
811
)
912

1013
// Watcher ...
@@ -18,27 +21,37 @@ type Watcher struct {
1821
}
1922

2023
// NewWatcher ...
21-
func NewWatcher(pollInterval int, watchItems []string, ignoreItems []string, extensions []string) *Watcher {
24+
func NewWatcher(pollInterval int, watchItems []string, ignoreItems []string, extensions []string) (*Watcher, error) {
2225
allowedExts := make(map[string]bool)
2326
for _, ext := range extensions {
2427
allowedExts["."+ext] = true
2528
}
2629

30+
watchMatches, err := resolveGlobMatches(watchItems)
31+
if err != nil {
32+
return nil, err
33+
}
34+
35+
ignoreMatches, err := resolveGlobMatches(ignoreItems)
36+
if err != nil {
37+
return nil, err
38+
}
39+
2740
return &Watcher{
2841
Events: make(chan string),
2942
Errors: make(chan error),
3043
PollInterval: pollInterval,
31-
WatchItems: watchItems,
32-
IgnoreItems: ignoreItems,
44+
WatchItems: watchMatches,
45+
IgnoreItems: ignoreMatches,
3346
AllowedExtensions: allowedExts,
34-
}
47+
}, nil
3548
}
3649

3750
var startTime = time.Now()
3851
var errDetectedChange = errors.New("done")
3952

4053
// Watch ...
41-
func (w *Watcher) Watch() { // nolint: gocyclo
54+
func (w *Watcher) Watch() {
4255
for {
4356
for i := range w.WatchItems {
4457
fileChanged, err := w.scanChange(w.WatchItems[i])
@@ -93,3 +106,19 @@ func (w *Watcher) scanChange(watchPath string) (string, error) {
93106

94107
return fileChanged, nil
95108
}
109+
110+
func resolveGlobMatches(paths []string) ([]string, error) {
111+
var result []string
112+
113+
for _, path := range paths {
114+
matches, err := zglob.Glob(path)
115+
if err != nil {
116+
return nil, fmt.Errorf("couldn't resolve glob path %s: %v", path, err)
117+
}
118+
119+
logger.Debugf("Resolved glob path %s: %v", path, matches)
120+
result = append(result, matches...)
121+
}
122+
123+
return result, nil
124+
}

0 commit comments

Comments
 (0)