-
Notifications
You must be signed in to change notification settings - Fork 24
/
file.go
55 lines (45 loc) · 1.53 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
package notifier
import (
filesystem "github.com/fluxninja/aperture/v2/pkg/file-system"
"github.com/fluxninja/aperture/v2/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.KeyBase
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{
KeyBase: notifiers.NewKeyBase(key),
path: fi.GetFilePath(),
fileInfo: fi,
}
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()
}
}