This repository has been archived by the owner on Aug 3, 2022. It is now read-only.
/
opts.go
169 lines (134 loc) · 2.88 KB
/
opts.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package opts
import (
"fmt"
"sync"
"syscall"
"go.uber.org/zap"
)
const (
// DefaultVerbose ...
DefaultVerbose = false
// DefaultTermSignal is the signal to term the agent.
DefaultTermSignal = syscall.SIGTERM
// DefaultReloadSignal is the default signal for reload.
DefaultReloadSignal = syscall.SIGHUP
// DefaultKillSignal is the default signal for termination.
DefaultKillSignal = syscall.SIGINT
)
var (
// ErrNotFound signals that this option is not set.
ErrNotFound = fmt.Errorf("option not found")
)
// Opt ...
type Opt int
const (
// Verbose ...
Verbose Opt = iota
// ReloadSignal ...
ReloadSignal
// TermSignal ...
TermSignal
// KillSignal ...
KillSignal
// Logger ...
Logger
)
// Opts ...
type Opts interface {
// Get ...
Get(Opt) (interface{}, error)
// Set ...
Set(Opt, interface{})
// Configure ...
Configure(...OptFunc)
}
// DefaultOpts ...
type DefaultOpts interface {
// Verbose ...
Verbose() bool
// ReloadSignal ...
ReloadSignal() syscall.Signal
// TermSignal ...
TermSignal() syscall.Signal
// KillSignal ...
KillSignal() syscall.Signal
Opts
}
// OptFunc is an option
type OptFunc func(Opts)
// Options is default options structure.
type Options struct {
opts map[Opt]interface{}
sync.RWMutex
}
// DefaultOptions are a collection of default options.
type DefaultOptions struct {
Options
}
// New returns a new instance of the options.
func New(opts ...OptFunc) Opts {
o := new(Options)
o.Configure(opts...)
return o
}
// NewDefaultOpts returns options with a default configuration.
func NewDefaultOpts(opts ...OptFunc) DefaultOpts {
o := new(DefaultOptions)
o.Configure(opts...)
o.Set(Verbose, DefaultVerbose)
o.Set(ReloadSignal, DefaultReloadSignal)
o.Set(TermSignal, DefaultTermSignal)
o.Set(KillSignal, DefaultKillSignal)
return o
}
// Verbose ...
func (o *DefaultOptions) Verbose() bool {
v, _ := o.Get(Verbose)
return v.(bool)
}
// ReloadSignal ...
func (o *DefaultOptions) ReloadSignal() syscall.Signal {
v, _ := o.Get(ReloadSignal)
return v.(syscall.Signal)
}
// TermSignal ...
func (o *DefaultOptions) TermSignal() syscall.Signal {
v, _ := o.Get(TermSignal)
return v.(syscall.Signal)
}
// KillSignal ...
func (o *DefaultOptions) KillSignal() syscall.Signal {
v, _ := o.Get(KillSignal)
return v.(syscall.Signal)
}
// WithLogger is setting a logger for options.
func WithLogger(logger *zap.Logger) OptFunc {
return func(opts Opts) {
opts.Set(Logger, logger)
}
}
// Get ...
func (o *Options) Get(opt Opt) (interface{}, error) {
o.RLock()
defer o.RUnlock()
v, ok := o.opts[opt]
if !ok {
return nil, ErrNotFound
}
return v, nil
}
// Set ...
func (o *Options) Set(opt Opt, v interface{}) {
o.Lock()
defer o.Unlock()
o.opts[opt] = v
}
// Configure os configuring the options.
func (o *Options) Configure(opts ...OptFunc) {
if o.opts == nil {
o.opts = make(map[Opt]interface{})
}
for _, opt := range opts {
opt(o)
}
}