diff options
| author | RanXom <saiyedshizain@gmail.com> | 2026-07-14 12:03:34 +0530 |
|---|---|---|
| committer | RanXom <saiyedshizain@gmail.com> | 2026-07-14 12:03:34 +0530 |
| commit | b336f4a1328e24ac77a1cfee8c52b18caa555620 (patch) | |
| tree | 991aecc2f1366ea621f5866f03913851db566c13 /internal | |
| parent | 9fd2f9eabe93713c0e8fb7dd094e77109db84265 (diff) | |
| download | galleryd-b336f4a1328e24ac77a1cfee8c52b18caa555620.tar.xz galleryd-b336f4a1328e24ac77a1cfee8c52b18caa555620.zip | |
feat(watcher): watch configured directories
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/watcher/config.go | 2 | ||||
| -rw-r--r-- | internal/watcher/fsnotify.go | 62 | ||||
| -rw-r--r-- | internal/watcher/watcher.go | 10 |
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 -} |