From 4cfd9930717beb30aaf28caccf397eba11834954 Mon Sep 17 00:00:00 2001 From: martin bergqlin Date: Tue, 11 Nov 2025 13:22:54 +0100 Subject: [PATCH] Refactor file watching implementation and update dependencies - Replaced `github.com/radovskyb/watcher` with `github.com/fsnotify/fsnotify` for file watching functionality. - Updated Go version to 1.25 in `go.mod` and CI configuration. - Adjusted linting configuration to use the latest version of `golangci-lint`. - Modified various file closing operations to handle errors gracefully. --- .github/workflows/go.yml | 7 +++---- .golangci.yml | 4 +--- config.go | 33 +++++++++++++++++++++------------ go.mod | 11 +++++++++-- go.sum | 6 ++++-- parsers/file.go | 2 +- parsers/k8s-volume.go | 2 +- parsers/url.go | 2 +- 8 files changed, 41 insertions(+), 26 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 7b77550..a289294 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -15,7 +15,7 @@ jobs: - name: Set up Go 1.x uses: actions/setup-go@v2 with: - go-version: ^1.14 + go-version: ^1.25 id: go - name: Check out code into the Go module directory @@ -32,6 +32,5 @@ jobs: run: go test -race -cover -v ./... - name: golangci-lint - uses: golangci/golangci-lint-action@v2 - with: - version: v1.49 + uses: golangci/golangci-lint-action@v9 + diff --git a/.golangci.yml b/.golangci.yml index 3afbc89..aed46c5 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,13 +1,11 @@ -run: +version: '2' linters: enable: - errcheck - errorlint - - gosimple - govet - ineffassign - - typecheck - unused - cyclop - durationcheck diff --git a/config.go b/config.go index cc1a79e..8d58d41 100644 --- a/config.go +++ b/config.go @@ -5,7 +5,7 @@ import ( "reflect" "sync" - "github.com/radovskyb/watcher" + "github.com/fsnotify/fsnotify" ) const configStructTagName string = "config" @@ -26,7 +26,7 @@ type Config struct { binders []reflect.Value cache *Values errch chan error - watch *watcher.Watcher + watch *fsnotify.Watcher m sync.Mutex } @@ -49,7 +49,7 @@ func New(opts ...Option) *Config { // Close cleans up channels and file watches. func (c *Config) Close() { if c.watch != nil { - c.watch.Close() + _ = c.watch.Close() } close(c.errch) @@ -273,19 +273,28 @@ func isSliceEqual(as interface{}, bs interface{}) bool { return true } -func newFileWatcher(fn func(), errfn func(error)) *watcher.Watcher { - w := watcher.New() - w.SetMaxEvents(1) - w.FilterOps(watcher.Write) +func newFileWatcher(fn func(), errfn func(error)) *fsnotify.Watcher { + w, err := fsnotify.NewWatcher() + if err != nil { + errfn(err) + return nil + } go func() { for { select { - case <-w.Event: - fn() - case <-w.Closed: - return - case err := <-w.Error: + case event, ok := <-w.Events: + if !ok { + return + } + // Only trigger on write events (file modifications) + if event.Has(fsnotify.Write) { + fn() + } + case err, ok := <-w.Errors: + if !ok { + return + } errfn(err) } } diff --git a/go.mod b/go.mod index 9906d97..89cabab 100644 --- a/go.mod +++ b/go.mod @@ -1,9 +1,16 @@ module github.com/ourstudio-se/binder -go 1.14 +go 1.25 require ( - github.com/radovskyb/watcher v1.0.7 + github.com/fsnotify/fsnotify v1.8.0 github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.8.0 ) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + golang.org/x/sys v0.13.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum index 959b2b1..852769d 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,10 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M= +github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/radovskyb/watcher v1.0.7 h1:AYePLih6dpmS32vlHfhCeli8127LzkIgwJGcwwe8tUE= -github.com/radovskyb/watcher v1.0.7/go.mod h1:78okwvY5wPdzcb1UYnip1pvrZNIVEIh/Cm+ZuvsUYIg= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -12,6 +12,8 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/parsers/file.go b/parsers/file.go index 22535b6..aeec9bc 100644 --- a/parsers/file.go +++ b/parsers/file.go @@ -24,7 +24,7 @@ func (p *FileParser) Parse() (map[string]interface{}, error) { return nil, err } - defer h.Close() + defer func() { _ = h.Close() }() kvp := NewKeyValueParser(h, WithKeyValueSeparator(p.sep)) return kvp.Parse() diff --git a/parsers/k8s-volume.go b/parsers/k8s-volume.go index c4c399f..832f562 100644 --- a/parsers/k8s-volume.go +++ b/parsers/k8s-volume.go @@ -22,7 +22,7 @@ func (p *KubernetesVolumeParser) Parse() (map[string]interface{}, error) { } key := filepath.Base(path) - b, err := os.ReadFile(path) + b, err := os.ReadFile(path) // #nosec G304 -- path is controlled by user-provided volume mount path if err != nil { return nil } diff --git a/parsers/url.go b/parsers/url.go index 31a5e5a..db33430 100644 --- a/parsers/url.go +++ b/parsers/url.go @@ -62,7 +62,7 @@ func (p *RemoteFileParser) Parse() (map[string]interface{}, error) { return nil, err } - defer resp.Body.Close() + defer func() { _ = resp.Body.Close() }() if resp.StatusCode >= 400 { return nil, fmt.Errorf("remote server returned unsuccessful status code: %d", resp.StatusCode)