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 --- go.mod | 2 ++ go.sum | 4 +++ internal/watcher/config.go | 2 +- internal/watcher/fsnotify.go | 62 ++++++++++++++++++++++++++++++++++++++++++++ internal/watcher/watcher.go | 10 ------- 5 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 internal/watcher/fsnotify.go diff --git a/go.mod b/go.mod index 0496f5f..96e8013 100644 --- a/go.mod +++ b/go.mod @@ -7,4 +7,6 @@ require ( github.com/disintegration/imaging v1.6.2 github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd golang.org/x/image v0.43.0 + github.com/fsnotify/fsnotify v1.10.1 + golang.org/x/sys v0.13.0 // indirect ) diff --git a/go.sum b/go.sum index ef997d8..170a81e 100644 --- a/go.sum +++ b/go.sum @@ -2,9 +2,13 @@ github.com/chai2010/webp v1.4.0 h1:6DA2pkkRUPnbOHvvsmGI3He1hBKf/bkRlniAiSGuEko= github.com/chai2010/webp v1.4.0/go.mod h1:0XVwvZWdjjdxpUEIf7b9g9VkHFnInUSYujwqTLEuldU= github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c= github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4= +github.com/fsnotify/fsnotify v1.10.1 h1:b0/UzAf9yR5rhf3RPm9gf3ehBPpf0oZKIjtpKrx59Ho= +github.com/fsnotify/fsnotify v1.10.1/go.mod h1:TLheqan6HD6GBK6PrDWyDPBaEV8LspOxvPSjC+bVfgo= github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd h1:CmH9+J6ZSsIjUK3dcGsnCnO41eRBOnY12zwkn5qVwgc= github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd/go.mod h1:hPqNNc0+uJM6H+SuU8sEs5K5IQeKccPqeSjfgcKGgPk= golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/image v0.43.0 h1:FLxcP4ec2350nTfOC8ysKtqYSIFbk/QGjw1ZHNP4tsY= golang.org/x/image v0.43.0/go.mod h1:rrpelvGFt+kLPAjPM4HeWPgrl0FtafueU//e5N0qk/Q= +golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 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 -} -- cgit v1.2.3