diff --git a/go.mod b/go.mod index 4bc4f396d8..b0d3b86445 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/apache/camel-k/v2/pkg/kamelet/repository v0.0.0 github.com/container-tools/spectrum v0.6.18 github.com/evanphx/json-patch v5.6.0+incompatible + github.com/fsnotify/fsnotify v1.6.0 github.com/gertd/go-pluralize v0.2.1 github.com/go-logr/logr v1.2.4 github.com/google/go-containerregistry v0.13.0 @@ -26,7 +27,6 @@ require ( github.com/prometheus/client_golang v1.15.1 github.com/prometheus/client_model v0.4.0 github.com/prometheus/common v0.44.0 - github.com/radovskyb/watcher v1.0.7 github.com/redhat-developer/service-binding-operator v1.3.4 github.com/rs/xid v1.5.0 github.com/scylladb/go-set v1.0.2 @@ -86,7 +86,6 @@ require ( github.com/docker/docker-credential-helpers v0.7.0 // indirect github.com/emicklei/go-restful/v3 v3.9.0 // indirect github.com/evanphx/json-patch/v5 v5.6.0 // indirect - github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/go-kit/log v0.2.1 // indirect github.com/go-logfmt/logfmt v0.5.1 // indirect github.com/go-logr/zapr v1.2.3 // indirect diff --git a/go.sum b/go.sum index 4061a245fe..5955b909d0 100644 --- a/go.sum +++ b/go.sum @@ -600,8 +600,6 @@ github.com/prometheus/procfs v0.9.0/go.mod h1:+pB4zwohETzFnmlpe6yd2lSc+0/46IYZRB github.com/prometheus/statsd_exporter v0.21.0 h1:hA05Q5RFeIjgwKIYEdFd59xu5Wwaznf33yKI+pyX6T8= github.com/prometheus/statsd_exporter v0.21.0/go.mod h1:rbT83sZq2V+p73lHhPZfMc3MLCHmSHelCh9hSGYNLTQ= github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= -github.com/radovskyb/watcher v1.0.7 h1:AYePLih6dpmS32vlHfhCeli8127LzkIgwJGcwwe8tUE= -github.com/radovskyb/watcher v1.0.7/go.mod h1:78okwvY5wPdzcb1UYnip1pvrZNIVEIh/Cm+ZuvsUYIg= github.com/redhat-developer/service-binding-operator v1.3.4 h1:ApCkGtXSKWCrRBQZCo/wRJ+XZWLe1EOnPjYtGcYSSMQ= github.com/redhat-developer/service-binding-operator v1.3.4/go.mod h1:mS/deuQ5m8rMnY9NbJ+0eTB0sC7Oy8SibiRnuyD++Bc= github.com/rickb777/date v1.13.0 h1:+8AmwLuY1d/rldzdqvqTEg7107bZ8clW37x4nsdG3Hs= diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go index 091eee8cd4..738df033f0 100644 --- a/pkg/cmd/run.go +++ b/pkg/cmd/run.go @@ -449,10 +449,11 @@ func (o *runCmdOptions) syncIntegration(cmd *cobra.Command, c client.Client, sou return err } if ok { - changes, err := sync.File(o.Context, s) + changes, watcher, err := sync.File(o.Context, s) if err != nil { return err } + defer watcher.Close() go func() { for { select { diff --git a/pkg/util/sync/file.go b/pkg/util/sync/file.go index bddfed7989..eb0b60d6dd 100644 --- a/pkg/util/sync/file.go +++ b/pkg/util/sync/file.go @@ -20,38 +20,37 @@ package sync import ( "context" - "time" - "github.com/apache/camel-k/v2/pkg/util/log" - "github.com/radovskyb/watcher" + "github.com/fsnotify/fsnotify" ) // File returns a channel that signals each time the content of the file changes. -func File(ctx context.Context, path string) (<-chan bool, error) { - w := watcher.New() - if err := w.Add(path); err != nil { - return nil, err +func File(ctx context.Context, path string) (<-chan bool, *fsnotify.Watcher, error) { + watcher, err := fsnotify.NewWatcher() + if err != nil { + return nil, watcher, err } - w.FilterOps(watcher.Write) out := make(chan bool) + + // Start listening for events. go func() { for { select { case <-ctx.Done(): return - case <-w.Event: - out <- true + case event := <-watcher.Events: + if event.Has(fsnotify.Write) { + out <- true + } } } }() - go func() { - if err := w.Start(200 * time.Millisecond); err != nil { - log.Error(err, "Error while starting watcher") - close(out) - } - }() + err = watcher.Add(path) + if err != nil { + return nil, watcher, err + } - return out, nil + return out, watcher, nil } diff --git a/pkg/util/sync/file_test.go b/pkg/util/sync/file_test.go index 258c2fec1e..33d4a044f1 100644 --- a/pkg/util/sync/file_test.go +++ b/pkg/util/sync/file_test.go @@ -36,9 +36,12 @@ func TestFile(t *testing.T) { ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(100*time.Second)) defer cancel() - changes, err := File(ctx, file.Name()) + changes, watcher, err := File(ctx, file.Name()) + assert.NotNil(t, watcher) assert.Nil(t, err) + defer watcher.Close() + time.Sleep(100 * time.Millisecond) expectedNumChanges := 3 for i := 0; i < expectedNumChanges; i++ {