@@ -2,9 +2,12 @@ package main
22
33import (
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
3750var startTime = time .Now ()
3851var 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