Skip to content

Commit 593383a

Browse files
committed
Add WithAwaitInitialValue
1 parent e09b067 commit 593383a

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

retriever.go

+37-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cdretriever
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"time"
78

@@ -14,7 +15,12 @@ type Retriever struct {
1415
}
1516

1617
// NewRetriever creates a new CentralDogma retriever.
17-
func NewRetriever(baseURL, token, project, repo, path string) (*Retriever, error) {
18+
func NewRetriever(baseURL, token, project, repo, path string, opts ...Option) (*Retriever, error) {
19+
cfg := &config{}
20+
for _, opt := range opts {
21+
opt(cfg)
22+
}
23+
1824
c, err := centraldogma.NewClientWithToken(baseURL, token, nil)
1925
if err != nil {
2026
return nil, fmt.Errorf("cdretriever: failed to create a new CentralDogma client: %w", err)
@@ -28,8 +34,10 @@ func NewRetriever(baseURL, token, project, repo, path string) (*Retriever, error
2834
return nil, fmt.Errorf("cdretriever: failed to create a new CentralDogma watcher: %w", err)
2935
}
3036

31-
if result := watcher.AwaitInitialValueWith(30 * time.Second); result.Err != nil {
32-
return nil, fmt.Errorf("cdretriever: failed to retrieve the initial value: %w", result.Err)
37+
if cfg.awaitInitialValueWith != nil {
38+
if result := watcher.AwaitInitialValueWith(*cfg.awaitInitialValueWith); result.Err != nil {
39+
return nil, &ErrAwaitInitialValue{cause: result.Err}
40+
}
3341
}
3442

3543
return &Retriever{Watcher: watcher}, nil
@@ -50,3 +58,29 @@ func (r *Retriever) Close() error {
5058
r.Watcher.Close()
5159
return nil
5260
}
61+
62+
type Option func(*config)
63+
64+
type config struct {
65+
awaitInitialValueWith *time.Duration
66+
}
67+
68+
func WithAwaitInitialValue(dur time.Duration) Option {
69+
return func(c *config) {
70+
c.awaitInitialValueWith = &dur
71+
}
72+
}
73+
74+
func IsErrAwaitInitialValue(err error) bool {
75+
var errAwaitInitialValue *ErrAwaitInitialValue
76+
ok := errors.As(err, &errAwaitInitialValue)
77+
return ok
78+
}
79+
80+
type ErrAwaitInitialValue struct {
81+
cause error
82+
}
83+
84+
func (e *ErrAwaitInitialValue) Error() string {
85+
return fmt.Sprintf("cdretriever: failed to retrieve the initial value: %s", e.cause)
86+
}

retriever_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"os"
66
"testing"
7+
"time"
78

89
"github.com/zzerjae/cdretriever"
910

@@ -45,3 +46,17 @@ func TestRetriever_Retrieve(t *testing.T) {
4546

4647
_ = r.Close()
4748
}
49+
50+
func TestRetriever_FailedToAwaitInitialValue(t *testing.T) {
51+
_, err := cdretriever.NewRetriever(
52+
"invalid-base-url",
53+
"invalid-token",
54+
"invalid-project",
55+
"invalid-repo",
56+
"flag-config.yaml",
57+
cdretriever.WithAwaitInitialValue(time.Second),
58+
)
59+
if err != nil {
60+
assert.True(t, cdretriever.IsErrAwaitInitialValue(err))
61+
}
62+
}

0 commit comments

Comments
 (0)