@@ -2,6 +2,7 @@ package cdretriever
2
2
3
3
import (
4
4
"context"
5
+ "errors"
5
6
"fmt"
6
7
"time"
7
8
@@ -14,7 +15,12 @@ type Retriever struct {
14
15
}
15
16
16
17
// 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
+
18
24
c , err := centraldogma .NewClientWithToken (baseURL , token , nil )
19
25
if err != nil {
20
26
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
28
34
return nil , fmt .Errorf ("cdretriever: failed to create a new CentralDogma watcher: %w" , err )
29
35
}
30
36
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
+ }
33
41
}
34
42
35
43
return & Retriever {Watcher : watcher }, nil
@@ -50,3 +58,29 @@ func (r *Retriever) Close() error {
50
58
r .Watcher .Close ()
51
59
return nil
52
60
}
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
+ }
0 commit comments