forked from nginx-proxy/docker-gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
85 lines (73 loc) · 1.5 KB
/
config.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
package dockergen
import (
"errors"
"strings"
"time"
"github.com/fsouza/go-dockerclient"
)
type Config struct {
Template string
Dest string
Watch bool
Wait *Wait
NotifyCmd string
NotifyOutput bool
NotifyContainers map[string]docker.Signal
OnlyExposed bool
OnlyPublished bool
IncludeStopped bool
Interval int
KeepBlankLines bool
}
type ConfigFile struct {
Config []Config
}
func (c *ConfigFile) FilterWatches() ConfigFile {
configWithWatches := []Config{}
for _, config := range c.Config {
if config.Watch {
configWithWatches = append(configWithWatches, config)
}
}
return ConfigFile{
Config: configWithWatches,
}
}
type Wait struct {
Min time.Duration
Max time.Duration
}
func (w *Wait) UnmarshalText(text []byte) error {
wait, err := ParseWait(string(text))
if err == nil {
w.Min, w.Max = wait.Min, wait.Max
}
return err
}
func ParseWait(s string) (*Wait, error) {
if len(strings.TrimSpace(s)) < 1 {
return &Wait{0, 0}, nil
}
parts := strings.Split(s, ":")
var (
min time.Duration
max time.Duration
err error
)
min, err = time.ParseDuration(strings.TrimSpace(parts[0]))
if err != nil {
return nil, err
}
if len(parts) > 1 {
max, err = time.ParseDuration(strings.TrimSpace(parts[1]))
if err != nil {
return nil, err
}
if max < min {
return nil, errors.New("Invalid wait interval: max must be larger than min")
}
} else {
max = 4 * min
}
return &Wait{min, max}, nil
}