-
Notifications
You must be signed in to change notification settings - Fork 0
/
retriever.go
86 lines (69 loc) · 2.02 KB
/
retriever.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package cdretriever
import (
"context"
"errors"
"fmt"
"time"
"go.linecorp.com/centraldogma"
)
// Retriever is a configuration struct for a CentralDogma retriever.
type Retriever struct {
Watcher *centraldogma.Watcher
}
// NewRetriever creates a new CentralDogma retriever.
func NewRetriever(baseURL, token, project, repo, path string, opts ...Option) (*Retriever, error) {
cfg := &config{}
for _, opt := range opts {
opt(cfg)
}
c, err := centraldogma.NewClientWithToken(baseURL, token, nil)
if err != nil {
return nil, fmt.Errorf("cdretriever: failed to create a new CentralDogma client: %w", err)
}
watcher, err := c.FileWatcher(project, repo, ¢raldogma.Query{
Path: path,
Type: centraldogma.Identity,
})
if err != nil {
return nil, fmt.Errorf("cdretriever: failed to create a new CentralDogma watcher: %w", err)
}
if cfg.awaitInitialValueWith != nil {
if result := watcher.AwaitInitialValueWith(*cfg.awaitInitialValueWith); result.Err != nil {
return nil, &ErrAwaitInitialValue{cause: result.Err}
}
}
return &Retriever{Watcher: watcher}, nil
}
// Retrieve retrieves the configuration from CentralDogma.
func (r *Retriever) Retrieve(_ context.Context) ([]byte, error) {
result := r.Watcher.Latest()
if result.Err != nil {
return nil, fmt.Errorf("cdretriever: failed to retrieve the latest value: %w", result.Err)
}
return result.Entry.Content, nil
}
// Close closes the CentralDogma retriever.
func (r *Retriever) Close() error {
r.Watcher.Close()
return nil
}
type Option func(*config)
type config struct {
awaitInitialValueWith *time.Duration
}
func WithAwaitInitialValue(dur time.Duration) Option {
return func(c *config) {
c.awaitInitialValueWith = &dur
}
}
func IsErrAwaitInitialValue(err error) bool {
var errAwaitInitialValue *ErrAwaitInitialValue
ok := errors.As(err, &errAwaitInitialValue)
return ok
}
type ErrAwaitInitialValue struct {
cause error
}
func (e *ErrAwaitInitialValue) Error() string {
return fmt.Sprintf("cdretriever: failed to retrieve the initial value: %s", e.cause)
}