aboutsummaryrefslogtreecommitdiff
path: root/internal/watcher/fsnotify.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/watcher/fsnotify.go')
-rw-r--r--internal/watcher/fsnotify.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/internal/watcher/fsnotify.go b/internal/watcher/fsnotify.go
new file mode 100644
index 0000000..e051c52
--- /dev/null
+++ b/internal/watcher/fsnotify.go
@@ -0,0 +1,62 @@
+package watcher
+
+import (
+ "context"
+ "fmt"
+ "io/fs"
+ "log"
+ "path/filepath"
+
+ "github.com/fsnotify/fsnotify"
+)
+
+// Run starts watching the configured roots.
+//
+// Filesystem events are currently logged for debugging.
+// Future commits will debounce these events and invoke OnChange.
+func (w *Watcher) Run(ctx context.Context) error {
+ fswatcher, err := fsnotify.NewWatcher()
+ if err != nil {
+ return fmt.Errorf("create watcher: %w", err)
+ }
+ defer func() {
+ _ = fswatcher.Close()
+ }()
+
+ 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 {
+ return err
+ }
+ }
+
+ for {
+ select {
+ case <-ctx.Done():
+ return nil
+
+ case event, ok := <-fswatcher.Events:
+ if !ok {
+ return nil
+ }
+
+ log.Printf("watcher: %v %s", event.Op, event.Name)
+
+ case err, ok := <-fswatcher.Errors:
+ if !ok {
+ return nil
+ }
+
+ log.Printf("watcher error: %v", err)
+ }
+ }
+}