-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
watcher.go
70 lines (58 loc) · 1.49 KB
/
watcher.go
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
//go:build linux
package namedpipe // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/namedpipe"
import (
"context"
"fmt"
"github.com/fsnotify/fsnotify"
)
// Watcher watches a file for writes, notifying via `C` when one is observed.
type Watcher struct {
C chan struct{}
watcher *fsnotify.Watcher
}
// NewWatcher creates a new watcher for the given path.
func NewWatcher(path string) (*Watcher, error) {
watcher, err := newWatcher(path)
if err != nil {
return nil, err
}
return &Watcher{
C: make(chan struct{}),
watcher: watcher,
}, nil
}
// Watch starts the watcher, sending a message to `C` when a write is observed.
func (w *Watcher) Watch(ctx context.Context) error {
defer func() {
w.watcher.Close()
close(w.C)
}()
for {
select {
case <-ctx.Done():
return nil
case event := <-w.watcher.Events:
if event.Op&fsnotify.Write == fsnotify.Write {
select {
case w.C <- struct{}{}:
case <-ctx.Done():
return nil
}
}
case err := <-w.watcher.Errors:
return fmt.Errorf("watcher error: %w", err)
}
}
}
func newWatcher(path string) (*fsnotify.Watcher, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, fmt.Errorf("failed to create watcher: %w", err)
}
if err := watcher.Add(path); err != nil {
return nil, fmt.Errorf("failed to add path to watcher: %w", err)
}
return watcher, nil
}