-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
config.go
100 lines (90 loc) · 2.33 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package agent
import (
"errors"
"fmt"
"strings"
"time"
"github.com/hashicorp/consul/agent/config"
)
var errInvalidHeaderFormat = errors.New("agent: invalid format of 'header' field")
func FixupCheckType(raw interface{}) error {
rawMap, ok := raw.(map[string]interface{})
if !ok {
return nil
}
// See https://github.com/hashicorp/consul/pull/3557 why we need this
// and why we should get rid of it. In Consul 1.0 we also didn't map
// Args correctly, so we ended up exposing (and need to carry forward)
// ScriptArgs, see https://github.com/hashicorp/consul/issues/3587.
config.TranslateKeys(rawMap, map[string]string{
"args": "ScriptArgs",
"script_args": "ScriptArgs",
"deregister_critical_service_after": "DeregisterCriticalServiceAfter",
"docker_container_id": "DockerContainerID",
"tls_skip_verify": "TLSSkipVerify",
"service_id": "ServiceID",
})
parseDuration := func(v interface{}) (time.Duration, error) {
if v == nil {
return 0, nil
}
switch x := v.(type) {
case time.Duration:
return x, nil
case float64:
return time.Duration(x), nil
case string:
return time.ParseDuration(x)
default:
return 0, fmt.Errorf("invalid format")
}
}
parseHeaderMap := func(v interface{}) (map[string][]string, error) {
if v == nil {
return nil, nil
}
vm, ok := v.(map[string]interface{})
if !ok {
return nil, errInvalidHeaderFormat
}
m := map[string][]string{}
for k, vv := range vm {
vs, ok := vv.([]interface{})
if !ok {
return nil, errInvalidHeaderFormat
}
for _, vs := range vs {
s, ok := vs.(string)
if !ok {
return nil, errInvalidHeaderFormat
}
m[k] = append(m[k], s)
}
}
return m, nil
}
for k, v := range rawMap {
switch strings.ToLower(k) {
case "header":
h, err := parseHeaderMap(v)
if err != nil {
return fmt.Errorf("invalid %q: %s", k, err)
}
rawMap[k] = h
case "ttl", "interval", "timeout", "deregistercriticalserviceafter":
d, err := parseDuration(v)
if err != nil {
return fmt.Errorf("invalid %q: %v", k, err)
}
rawMap[k] = d
}
}
return nil
}
func ParseMetaPair(raw string) (string, string) {
pair := strings.SplitN(raw, ":", 2)
if len(pair) == 2 {
return pair[0], pair[1]
}
return pair[0], ""
}