-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
60 lines (50 loc) · 1.1 KB
/
options.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
package s10s
import (
"errors"
"fmt"
"log/slog"
"net/http"
"os"
)
type ClientOption func(*Client) error
var DefaultOptions []ClientOption = []ClientOption{WithTokenFromEnv, WithoutLogging}
func WithLogger(logger *slog.Logger) ClientOption {
return func(c *Client) error {
c.l = logger
return nil
}
}
func WithEvents(em FireEventer) ClientOption {
return func(c *Client) error {
c.em = em
return nil
}
}
func WithHTTPClient(httpClient *http.Client) ClientOption {
return func(c *Client) error {
c.cfg.HTTPClient = httpClient
return nil
}
}
const tokenEnvName = "S10S_API_TOKEN" //nolint:gosec
func WithTokenFromEnv(c *Client) error {
token := os.Getenv(tokenEnvName)
if token == "" {
return fmt.Errorf("Cannot initialize, environment variable %q is unset", tokenEnvName)
}
c.token = token
return nil
}
func WithToken(token string) func(*Client) error {
return func(c *Client) error {
if token == "" {
return errors.New("Cannot initialize, token is empty")
}
c.token = token
return nil
}
}
func WithoutLogging(c *Client) error {
c.l = slog.New(&discardHandler{})
return nil
}