This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathwatcher.go
More file actions
76 lines (70 loc) · 1.88 KB
/
watcher.go
File metadata and controls
76 lines (70 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package sse
import (
"context"
"github.com/fsnotify/fsnotify"
"k8s.io/klog/v2"
)
func (n *Notifier) watchDevfileChanges(ctx context.Context, devfileFiles []string) error {
devfileWatcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
go func() {
for {
select {
case <-ctx.Done():
klog.V(2).Infof("context done, reason: %v", ctx.Err())
cErr := devfileWatcher.Close()
if cErr != nil {
klog.V(2).Infof("error closing devfileWater: %v", cErr)
}
return
case ev, ok := <-devfileWatcher.Events:
if !ok {
return
}
klog.V(7).Infof("event: %v", ev)
devfileContent, rErr := n.fsys.ReadFile(n.devfilePath)
if rErr != nil {
klog.V(1).Infof("unable to read Devfile at path %q: %v", n.devfilePath, rErr)
continue
}
n.eventsChan <- Event{
eventType: DevfileUpdated,
data: map[string]string{
"path": ev.Name,
"operation": ev.Op.String(),
"content": string(devfileContent),
},
}
if ev.Has(fsnotify.Remove) {
// For some reason, depending on the editor used to edit the file, changes would be detected only once.
// Workaround recommended is to re-add the path to the watcher.
// See https://github.com/fsnotify/fsnotify/issues/363
wErr := devfileWatcher.Remove(ev.Name)
if wErr != nil {
klog.V(7).Infof("error removing file watch: %v", wErr)
}
wErr = devfileWatcher.Add(ev.Name)
if wErr != nil {
klog.V(0).Infof("error re-adding file watch: %v", wErr)
}
}
case wErr, ok := <-devfileWatcher.Errors:
if !ok {
return
}
klog.V(0).Infof("error on file watch: %v", wErr)
}
}
}()
for _, f := range devfileFiles {
err = devfileWatcher.Add(f)
if err != nil {
klog.V(0).Infof("error adding watcher for path %q: %v", f, err)
} else {
klog.V(7).Infof("added watcher for path %q", f)
}
}
return nil
}