From b336f4a1328e24ac77a1cfee8c52b18caa555620 Mon Sep 17 00:00:00 2001 From: RanXom Date: Tue, 14 Jul 2026 12:03:34 +0530 Subject: feat(watcher): watch configured directories --- internal/watcher/fsnotify.go | 62 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 internal/watcher/fsnotify.go (limited to 'internal/watcher/fsnotify.go') 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) + } + } +} -- cgit v1.2.3 From d2c0c970f690d52ee3675f377a5eebc22f84afab Mon Sep 17 00:00:00 2001 From: RanXom Date: Tue, 14 Jul 2026 12:35:51 +0530 Subject: feat(watcher): watch newly created directories --- internal/watcher/fsnotify.go | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) (limited to 'internal/watcher/fsnotify.go') 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 -- cgit v1.2.3 From 9eb299c3ac08e2bea4dfe9e3355178566bcf0501 Mon Sep 17 00:00:00 2001 From: RanXom Date: Fri, 17 Jul 2026 00:36:31 +0530 Subject: refactor(watcher): isolate debounce loop --- internal/watcher/fsnotify.go | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'internal/watcher/fsnotify.go') diff --git a/internal/watcher/fsnotify.go b/internal/watcher/fsnotify.go index 7251907..b759c50 100644 --- a/internal/watcher/fsnotify.go +++ b/internal/watcher/fsnotify.go @@ -22,6 +22,8 @@ func (w *Watcher) Run(ctx context.Context) error { _ = fswatcher.Close() }() + go w.debounceLoop(ctx) + for _, root := range w.config.Roots { if err := watchTree(fswatcher, root); err != nil { return err @@ -40,6 +42,8 @@ func (w *Watcher) Run(ctx context.Context) error { log.Printf("watcher: %v %s", event.Op, event.Name) + w.notify() + if !event.Has(fsnotify.Create) { continue } -- cgit v1.2.3