Skip to content

Commit

Permalink
chore(deps): fsnotify instead of radovskyb/watcher
Browse files Browse the repository at this point in the history
Ref #4326
  • Loading branch information
squakez committed Jun 5, 2023
1 parent b37114c commit 134f966
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
33 changes: 16 additions & 17 deletions pkg/util/sync/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 4 additions & 1 deletion pkg/util/sync/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down

0 comments on commit 134f966

Please sign in to comment.