Skip to content

Commit

Permalink
Fix file watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
juliens authored Feb 9, 2024
1 parent d5cb9b5 commit 676de5f
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions pkg/provider/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"text/template"
Expand Down Expand Up @@ -47,18 +48,31 @@ func (p *Provider) Init() error {
// using the given configuration channel.
func (p *Provider) Provide(configurationChan chan<- dynamic.Message, pool *safe.Pool) error {
if p.Watch {
var watchItem string
var watchItems []string

switch {
case len(p.Directory) > 0:
watchItem = p.Directory
watchItems = append(watchItems, p.Directory)

fileList, err := os.ReadDir(p.Directory)
if err != nil {
return fmt.Errorf("unable to read directory %s: %w", p.Directory, err)
}

for _, entry := range fileList {
if entry.IsDir() {
// ignore sub-dir
continue
}
watchItems = append(watchItems, path.Join(p.Directory, entry.Name()))
}
case len(p.Filename) > 0:
watchItem = filepath.Dir(p.Filename)
watchItems = append(watchItems, filepath.Dir(p.Filename), p.Filename)
default:
return errors.New("error using file configuration provider, neither filename or directory defined")
}

if err := p.addWatcher(pool, watchItem, configurationChan, p.watcherCallback); err != nil {
if err := p.addWatcher(pool, watchItems, configurationChan, p.watcherCallback); err != nil {
return err
}
}
Expand Down Expand Up @@ -92,15 +106,18 @@ func (p *Provider) BuildConfiguration() (*dynamic.Configuration, error) {
return nil, errors.New("error using file configuration provider, neither filename or directory defined")
}

func (p *Provider) addWatcher(pool *safe.Pool, directory string, configurationChan chan<- dynamic.Message, callback func(chan<- dynamic.Message, fsnotify.Event)) error {
func (p *Provider) addWatcher(pool *safe.Pool, items []string, configurationChan chan<- dynamic.Message, callback func(chan<- dynamic.Message, fsnotify.Event)) error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return fmt.Errorf("error creating file watcher: %w", err)
}

err = watcher.Add(directory)
if err != nil {
return fmt.Errorf("error adding file watcher: %w", err)
for _, item := range items {
log.WithoutContext().Debugf("add watcher on: %s", item)
err = watcher.Add(item)
if err != nil {
return fmt.Errorf("error adding file watcher: %w", err)
}
}

// Process events
Expand Down

0 comments on commit 676de5f

Please sign in to comment.