-
Notifications
You must be signed in to change notification settings - Fork 25
/
file.go
56 lines (46 loc) · 1.52 KB
/
file.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
package notifier
import (
"github.com/fluxninja/aperture/pkg/filesystem"
"github.com/fluxninja/aperture/pkg/notifiers"
)
// KeyToFSNotifier holds the state of a notifier that writes raw/transformed contents of a watched file to another file.
type KeyToFSNotifier struct {
fileInfo *filesystem.FileInfo
notifiers.KeyNotifierBase
path string
}
// Make sure KeyToFSNotifier implements KeyNotifier.
var _ notifiers.KeyNotifier = (*KeyToFSNotifier)(nil)
// NewKeyToFSNotifier returns a new notifier that writes raw/transformed contents to another file.
func NewKeyToFSNotifier(key notifiers.Key, dir string, ext string) *KeyToFSNotifier {
fi := filesystem.NewFileInfo(dir, key.String(), ext)
n := &KeyToFSNotifier{
path: fi.GetFilePath(),
fileInfo: fi,
}
// nolint: typecheck
n.SetKey(key)
return n
}
// Start starts the key notifier.
func (n *KeyToFSNotifier) Start() error {
// Should we remove the file or write nil to it?
_ = n.fileInfo.RemoveFile()
return nil
}
// Stop stops the key notifier.
func (n *KeyToFSNotifier) Stop() error {
return nil
}
// Notify writes/removes to filesystem based on received event.
func (n *KeyToFSNotifier) Notify(event notifiers.Event) {
// Should the new fi be created on every event?
// Should it be the same directory as n.FileInfo.Directory?
fi := filesystem.NewFileInfo(n.fileInfo.GetDirectory(), event.Key.String(), n.fileInfo.GetFileExt())
switch event.Type {
case notifiers.Write:
_ = fi.WriteByteBufferToFile(event.Value)
case notifiers.Remove:
_ = fi.RemoveFile()
}
}