-
Notifications
You must be signed in to change notification settings - Fork 23
/
provider.go
57 lines (47 loc) · 1.4 KB
/
provider.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
package watcher
import (
"io/fs"
"os"
"path"
"go.uber.org/fx"
"github.com/fluxninja/aperture/v2/pkg/config"
etcdclient "github.com/fluxninja/aperture/v2/pkg/etcd/client"
"github.com/fluxninja/aperture/v2/pkg/log"
"github.com/fluxninja/aperture/v2/pkg/notifiers"
)
// Constructor holds fields to create an annotated instance of etcd Watcher.
type Constructor struct {
Name string
EtcdPath string
}
// Annotate creates an annotated instance of etcd Watcher.
func (c Constructor) Annotate() fx.Option {
name := ``
if c.Name != "" {
name = config.NameTag(c.Name)
}
return fx.Options(
fx.Provide(
fx.Annotate(
c.provideWatcher,
fx.ResultTags(name),
),
),
)
}
func (c Constructor) provideWatcher(client *etcdclient.Client, unmarshaller config.Unmarshaller, lifecycle fx.Lifecycle) (notifiers.Watcher, error) {
watcher, err := NewWatcher(client, c.EtcdPath)
if err != nil {
log.Error().Err(err).Msg("Failed to create etcd watcher")
return nil, err
}
// make test notifier directory and create a new PrefixToFS notifier on that directory
testNotifierPath := path.Join(config.DefaultTempDirectory, c.EtcdPath)
err = os.MkdirAll(testNotifierPath, fs.ModePerm)
if err != nil {
log.Error().Err(err).Str("dir", testNotifierPath).Msg("Unable to create test notifier directory")
return nil, err
}
lifecycle.Append(fx.StartStopHook(watcher.Start, watcher.Stop))
return watcher, nil
}