This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
86 lines (75 loc) · 1.77 KB
/
config.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 zerosdk
import (
"fmt"
"net/http"
"time"
)
type Option func(*config)
type config struct {
clusterAPIEndpoint string
connectAPIEndpoint string
apiToken string
httpClient *http.Client
downloadURLCacheTTL time.Duration
}
// WithClusterAPIEndpoint sets the cluster API endpoint
func WithClusterAPIEndpoint(endpoint string) Option {
return func(cfg *config) {
cfg.clusterAPIEndpoint = endpoint
}
}
// WithConnectAPIEndpoint sets the connect API endpoint
func WithConnectAPIEndpoint(endpoint string) Option {
return func(cfg *config) {
cfg.connectAPIEndpoint = endpoint
}
}
// WithAPIToken sets the API token
func WithAPIToken(token string) Option {
return func(cfg *config) {
cfg.apiToken = token
}
}
// WithHTTPClient sets the HTTP client
func WithHTTPClient(client *http.Client) Option {
return func(cfg *config) {
cfg.httpClient = client
}
}
// WithDownloadURLCacheTTL sets the minimum TTL for download URL cache entries
func WithDownloadURLCacheTTL(ttl time.Duration) Option {
return func(cfg *config) {
cfg.downloadURLCacheTTL = ttl
}
}
func newConfig(opts ...Option) (*config, error) {
cfg := new(config)
for _, opt := range []Option{
WithHTTPClient(http.DefaultClient),
WithDownloadURLCacheTTL(15 * time.Minute),
} {
opt(cfg)
}
for _, opt := range opts {
opt(cfg)
}
if err := cfg.validate(); err != nil {
return nil, err
}
return cfg, nil
}
func (c *config) validate() error {
if c.clusterAPIEndpoint == "" {
return fmt.Errorf("cluster API endpoint is required")
}
if c.connectAPIEndpoint == "" {
return fmt.Errorf("connect API endpoint is required")
}
if c.apiToken == "" {
return fmt.Errorf("API token is required")
}
if c.httpClient == nil {
return fmt.Errorf("HTTP client is required")
}
return nil
}