-
Notifications
You must be signed in to change notification settings - Fork 9
/
options.go
51 lines (41 loc) · 946 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
51
package graceful
import "context"
// Option interface for configuration.
type Option interface {
Apply(*Options)
}
// OptionFunc is a function that configures a graceful shutdown.
type OptionFunc func(*Options)
// Apply calls f(option)
func (f OptionFunc) Apply(option *Options) {
f(option)
}
// Options for graceful shutdown
type Options struct {
ctx context.Context
logger Logger
}
// WithContext custom context
func WithContext(ctx context.Context) Option {
return OptionFunc(func(o *Options) {
o.ctx = ctx
})
}
// WithLogger custom logger
func WithLogger(logger Logger) Option {
return OptionFunc(func(o *Options) {
o.logger = logger
})
}
func newOptions(opts ...Option) Options {
defaultOpts := Options{
ctx: context.Background(),
logger: NewLogger(),
}
// Loop through each option
for _, opt := range opts {
// Call the option giving the instantiated
opt.Apply(&defaultOpts)
}
return defaultOpts
}