forked from kelseyhightower/confd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_funcs.go
213 lines (185 loc) · 4.89 KB
/
template_funcs.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package template
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net"
"os"
"path"
"sort"
"strings"
"time"
"github.com/kelseyhightower/memkv"
)
func newFuncMap() map[string]interface{} {
m := make(map[string]interface{})
m["base"] = path.Base
m["split"] = strings.Split
m["json"] = UnmarshalJsonObject
m["jsonArray"] = UnmarshalJsonArray
m["dir"] = path.Dir
m["map"] = CreateMap
m["getenv"] = Getenv
m["join"] = strings.Join
m["datetime"] = time.Now
m["toUpper"] = strings.ToUpper
m["toLower"] = strings.ToLower
m["contains"] = strings.Contains
m["replace"] = strings.Replace
m["trimSuffix"] = strings.TrimSuffix
m["lookupIP"] = LookupIP
m["lookupSRV"] = LookupSRV
m["fileExists"] = isFileExist
m["base64Encode"] = Base64Encode
m["base64Decode"] = Base64Decode
m["reverse"] = Reverse
m["sortByLength"] = SortByLength
m["sortKVByLength"] = SortKVByLength
m["add"] = func(a, b int) int { return a + b }
m["sub"] = func(a, b int) int { return a - b }
m["div"] = func(a, b int) int { return a / b }
m["mod"] = func(a, b int) int { return a % b }
m["mul"] = func(a, b int) int { return a * b }
m["seq"] = Seq
return m
}
func addFuncs(out, in map[string]interface{}) {
for name, fn := range in {
out[name] = fn
}
}
// Seq creates a sequence of integers. It's named and used as GNU's seq.
// Seq takes the first and the last element as arguments. So Seq(3, 5) will generate [3,4,5]
func Seq(first, last int) []int {
var arr []int
for i := first; i <= last; i++ {
arr = append(arr, i)
}
return arr
}
type byLengthKV []memkv.KVPair
func (s byLengthKV) Len() int {
return len(s)
}
func (s byLengthKV) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s byLengthKV) Less(i, j int) bool {
return len(s[i].Key) < len(s[j].Key)
}
func SortKVByLength(values []memkv.KVPair) []memkv.KVPair {
sort.Sort(byLengthKV(values))
return values
}
type byLength []string
func (s byLength) Len() int {
return len(s)
}
func (s byLength) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s byLength) Less(i, j int) bool {
return len(s[i]) < len(s[j])
}
func SortByLength(values []string) []string {
sort.Sort(byLength(values))
return values
}
//Reverse returns the array in reversed order
//works with []string and []KVPair
func Reverse(values interface{}) interface{} {
switch values.(type) {
case []string:
v := values.([]string)
for left, right := 0, len(v)-1; left < right; left, right = left+1, right-1 {
v[left], v[right] = v[right], v[left]
}
case []memkv.KVPair:
v := values.([]memkv.KVPair)
for left, right := 0, len(v)-1; left < right; left, right = left+1, right-1 {
v[left], v[right] = v[right], v[left]
}
}
return values
}
// Getenv retrieves the value of the environment variable named by the key.
// It returns the value, which will the default value if the variable is not present.
// If no default value was given - returns "".
func Getenv(key string, v ...string) string {
defaultValue := ""
if len(v) > 0 {
defaultValue = v[0]
}
value := os.Getenv(key)
if value == "" {
return defaultValue
}
return value
}
// CreateMap creates a key-value map of string -> interface{}
// The i'th is the key and the i+1 is the value
func CreateMap(values ...interface{}) (map[string]interface{}, error) {
if len(values)%2 != 0 {
return nil, errors.New("invalid map call")
}
dict := make(map[string]interface{}, len(values)/2)
for i := 0; i < len(values); i += 2 {
key, ok := values[i].(string)
if !ok {
return nil, errors.New("map keys must be strings")
}
dict[key] = values[i+1]
}
return dict, nil
}
func UnmarshalJsonObject(data string) (map[string]interface{}, error) {
var ret map[string]interface{}
err := json.Unmarshal([]byte(data), &ret)
return ret, err
}
func UnmarshalJsonArray(data string) ([]interface{}, error) {
var ret []interface{}
err := json.Unmarshal([]byte(data), &ret)
return ret, err
}
func LookupIP(data string) []string {
ips, err := net.LookupIP(data)
if err != nil {
return nil
}
// "Cast" IPs into strings and sort the array
ipStrings := make([]string, len(ips))
for i, ip := range ips {
ipStrings[i] = ip.String()
}
sort.Strings(ipStrings)
return ipStrings
}
type sortSRV []*net.SRV
func (s sortSRV) Len() int {
return len(s)
}
func (s sortSRV) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s sortSRV) Less(i, j int) bool {
str1 := fmt.Sprintf("%s%d%d%d", s[i].Target, s[i].Port, s[i].Priority, s[i].Weight)
str2 := fmt.Sprintf("%s%d%d%d", s[j].Target, s[j].Port, s[j].Priority, s[j].Weight)
return str1 < str2
}
func LookupSRV(service, proto, name string) []*net.SRV {
_, addrs, err := net.LookupSRV(service, proto, name)
if err != nil {
return []*net.SRV{}
}
sort.Sort(sortSRV(addrs))
return addrs
}
func Base64Encode(data string) string {
return base64.StdEncoding.EncodeToString([]byte(data))
}
func Base64Decode(data string) (string, error) {
s, err := base64.StdEncoding.DecodeString(data)
return string(s), err
}