-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.go
52 lines (43 loc) · 802 Bytes
/
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
package moonshot
import (
"errors"
"strings"
)
type Config struct {
Host string
APIKey string
Debug bool
}
const DefaultHost = "https://api.moonshot.cn"
func newConfigDefault() *Config {
return &Config{
Host: DefaultHost,
}
}
// NewConfig creates a new config
func NewConfig(opts ...Option) *Config {
cfg := newConfigDefault()
for _, opt := range opts {
opt(cfg)
}
return cfg
}
func (c *Config) preCheck() error {
if len(c.APIKey) == 0 {
return errors.New("API key is required")
}
return nil
}
type Option func(*Config)
// WithHost sets the host
func WithHost(host string) Option {
return func(c *Config) {
c.Host = strings.TrimSuffix(host, "/")
}
}
// WithAPIKey sets the API key
func WithAPIKey(key string) Option {
return func(c *Config) {
c.APIKey = key
}
}