forked from hashicorp/consul-template
/
wait.go
191 lines (154 loc) · 3.95 KB
/
wait.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package config
import (
"errors"
"fmt"
"strings"
"time"
)
var (
// ErrWaitStringEmpty is the error returned when wait is specified as an empty
// string.
ErrWaitStringEmpty = errors.New("wait: cannot be empty")
// ErrWaitInvalidFormat is the error returned when the wait is specified
// incorrectly.
ErrWaitInvalidFormat = errors.New("wait: invalid format")
// ErrWaitNegative is the error returned with the wait is negative.
ErrWaitNegative = errors.New("wait: cannot be negative")
// ErrWaitMinLTMax is the error returned with the minimum wait time is not
// less than the maximum wait time.
ErrWaitMinLTMax = errors.New("wait: min must be less than max")
)
// WaitConfig is the Min/Max duration used by the Watcher
type WaitConfig struct {
// Enabled determines if this wait is enabled.
Enabled *bool `mapstructure:"bool"`
// Min and Max are the minimum and maximum time, respectively, to wait for
// data changes before rendering a new template to disk.
Min *time.Duration `mapstructure:"min"`
Max *time.Duration `mapstructure:"max"`
}
// DefaultWaitConfig is the default configuration.
func DefaultWaitConfig() *WaitConfig {
return &WaitConfig{}
}
// Copy returns a deep copy of this configuration.
func (c *WaitConfig) Copy() *WaitConfig {
if c == nil {
return nil
}
var o WaitConfig
o.Enabled = c.Enabled
o.Min = c.Min
o.Max = c.Max
return &o
}
// Merge combines all values in this configuration with the values in the other
// configuration, with values in the other configuration taking precedence.
// Maps and slices are merged, most other values are overwritten. Complex
// structs define their own merge functionality.
func (c *WaitConfig) Merge(o *WaitConfig) *WaitConfig {
if c == nil {
if o == nil {
return nil
}
return o.Copy()
}
if o == nil {
return c.Copy()
}
r := c.Copy()
if o.Enabled != nil {
r.Enabled = o.Enabled
}
if o.Min != nil {
r.Min = o.Min
}
if o.Max != nil {
r.Max = o.Max
}
return r
}
// Finalize ensures there no nil pointers.
func (c *WaitConfig) Finalize() {
if c.Enabled == nil {
c.Enabled = Bool(TimeDurationPresent(c.Min))
}
if c.Min == nil {
c.Min = TimeDuration(0 * time.Second)
}
if c.Max == nil {
c.Max = TimeDuration(4 * *c.Min)
}
}
// GoString defines the printable version of this struct.
func (c *WaitConfig) GoString() string {
if c == nil {
return "(*WaitConfig)(nil)"
}
return fmt.Sprintf("&WaitConfig{"+
"Enabled:%s, "+
"Min:%s, "+
"Max:%s"+
"}",
BoolGoString(c.Enabled),
TimeDurationGoString(c.Min),
TimeDurationGoString(c.Max),
)
}
// ParseWaitConfig parses a string of the format `minimum(:maximum)` into a
// WaitConfig.
func ParseWaitConfig(s string) (*WaitConfig, error) {
s = strings.TrimSpace(s)
if len(s) < 1 {
return nil, ErrWaitStringEmpty
}
parts := strings.Split(s, ":")
var min, max time.Duration
var err error
switch len(parts) {
case 1:
min, err = time.ParseDuration(strings.TrimSpace(parts[0]))
if err != nil {
return nil, err
}
max = 4 * min
case 2:
min, err = time.ParseDuration(strings.TrimSpace(parts[0]))
if err != nil {
return nil, err
}
max, err = time.ParseDuration(strings.TrimSpace(parts[1]))
if err != nil {
return nil, err
}
default:
return nil, ErrWaitInvalidFormat
}
if min < 0 || max < 0 {
return nil, ErrWaitNegative
}
if max < min {
return nil, ErrWaitMinLTMax
}
var c WaitConfig
c.Min = TimeDuration(min)
c.Max = TimeDuration(max)
return &c, nil
}
// WaitVar implements the Flag.Value interface and allows the user to specify
// a watch interval using Go's flag parsing library.
type WaitVar WaitConfig
// Set sets the value in the format min[:max] for a wait timer.
func (w *WaitVar) Set(value string) error {
wait, err := ParseWaitConfig(value)
if err != nil {
return err
}
w.Min = wait.Min
w.Max = wait.Max
return nil
}
// String returns the string format for this wait variable
func (w *WaitVar) String() string {
return fmt.Sprintf("%s:%s", w.Min, w.Max)
}