aboutsummaryrefslogtreecommitdiff
path: root/internal/watcher
diff options
context:
space:
mode:
Diffstat (limited to 'internal/watcher')
-rw-r--r--internal/watcher/config.go2
-rw-r--r--internal/watcher/fsnotify.go62
-rw-r--r--internal/watcher/watcher.go10
3 files changed, 63 insertions, 11 deletions
diff --git a/internal/watcher/config.go b/internal/watcher/config.go
index ad12d0c..a59474f 100644
--- a/internal/watcher/config.go
+++ b/internal/watcher/config.go
@@ -2,7 +2,7 @@ package watcher
import "time"
-// Configures a filesystem watcher
+// Config configures a filesystem watcher
type Config struct {
Roots []string
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)
+ }
+ }
+}
diff --git a/internal/watcher/watcher.go b/internal/watcher/watcher.go
index 5465554..1ca8e37 100644
--- a/internal/watcher/watcher.go
+++ b/internal/watcher/watcher.go
@@ -1,7 +1,5 @@
package watcher
-import "context"
-
type Watcher struct {
config Config
}
@@ -11,11 +9,3 @@ func New(config Config) *Watcher {
config: config,
}
}
-
-// Run starts the watcher.
-//
-// Placeholder implementation.
-func (w *Watcher) Run(ctx context.Context) error {
- <-ctx.Done()
- return nil
-}