diff options
Diffstat (limited to 'internal/watcher')
| -rw-r--r-- | internal/watcher/fsnotify.go | 32 | ||||
| -rw-r--r-- | internal/watcher/watch.go | 26 |
2 files changed, 45 insertions, 13 deletions
diff --git a/internal/watcher/fsnotify.go b/internal/watcher/fsnotify.go index e051c52..7251907 100644 --- a/internal/watcher/fsnotify.go +++ b/internal/watcher/fsnotify.go @@ -3,9 +3,8 @@ package watcher import ( "context" "fmt" - "io/fs" "log" - "path/filepath" + "os" "github.com/fsnotify/fsnotify" ) @@ -24,17 +23,7 @@ func (w *Watcher) Run(ctx context.Context) error { }() for _, root := range w.config.Roots { - if err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { - if err != nil { - return err - } - - if !d.IsDir() { - return nil - } - - return fswatcher.Add(path) - }); err != nil { + if err := watchTree(fswatcher, root); err != nil { return err } } @@ -51,6 +40,23 @@ func (w *Watcher) Run(ctx context.Context) error { log.Printf("watcher: %v %s", event.Op, event.Name) + if !event.Has(fsnotify.Create) { + continue + } + + info, err := os.Stat(event.Name) + if err != nil { + continue + } + + if !info.IsDir() { + continue + } + + if err := watchTree(fswatcher, event.Name); err != nil { + log.Printf("watch new directory: %v", err) + } + case err, ok := <-fswatcher.Errors: if !ok { return nil diff --git a/internal/watcher/watch.go b/internal/watcher/watch.go new file mode 100644 index 0000000..95146ce --- /dev/null +++ b/internal/watcher/watch.go @@ -0,0 +1,26 @@ +package watcher + +import ( + "io/fs" + "path/filepath" + + "github.com/fsnotify/fsnotify" +) + +// watchTree recursively watches root and all of its subdirectories. +func watchTree( + w *fsnotify.Watcher, + root string, +) error { + return filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + + if !d.IsDir() { + return nil + } + + return w.Add(path) + }) +} |