-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
50 lines (42 loc) · 862 Bytes
/
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
package egothic
import (
"fmt"
"log"
)
// Options is a function that configures the egothic package.
type Options func(*egothicConfig)
type egothicConfig struct {
debug bool
logger *log.Logger
}
func (c *egothicConfig) apply(opts ...Options) {
for _, opt := range opts {
opt(c)
}
}
func newConfig(opts ...Options) *egothicConfig {
config := &egothicConfig{}
config.apply(opts...)
return config
}
func (c *egothicConfig) log(msg string) {
if c.debug {
if c.logger != nil {
c.logger.Println(msg)
} else {
fmt.Println(msg)
}
}
}
// WithDebug enables the debug mode for the egothic package.
func WithDebug() Options {
return func(c *egothicConfig) {
c.debug = true
}
}
// WithLogger sets the logger for the egothic package.
func WithLogger(logger *log.Logger) Options {
return func(c *egothicConfig) {
c.logger = logger
}
}