forked from openpitrix/libconfd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmpl_funcs.go
executable file
·365 lines (304 loc) · 8.72 KB
/
tmpl_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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Copyright confd. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE-confd file.
//go:generate go run gen_tmpl_func_list.go -output tmpl_funcs_zz.go
package libconfd
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net"
"os"
pathpkg "path"
"sort"
"strconv"
"strings"
"time"
)
type TemplateFunc struct {
FuncMap map[string]interface{}
Store *KVStore
PGPPrivateKey []byte
}
var _TemplateFunc_initFuncMap func(p *TemplateFunc) = nil
func NewTemplateFunc(store *KVStore, pgpPrivateKey []byte) *TemplateFunc {
p := &TemplateFunc{
FuncMap: map[string]interface{}{},
Store: store,
PGPPrivateKey: pgpPrivateKey,
}
if _TemplateFunc_initFuncMap == nil {
logger.Panic("_TemplateFunc_initFuncMap missing")
}
_TemplateFunc_initFuncMap(p)
return p
}
// ----------------------------------------------------------------------------
// KVStore
// ----------------------------------------------------------------------------
func (p TemplateFunc) Exists(key string) bool {
return p.Store.Exists(key)
}
func (p TemplateFunc) Ls(filepath string) []string {
return p.Store.List(filepath)
}
func (p TemplateFunc) Lsdir(filepath string) []string {
return p.Store.ListDir(filepath)
}
func (p TemplateFunc) Get(key string) (KVPair, error) {
if v, ok := p.Store.Get(key); ok {
return v, nil
}
return KVPair{}, fmt.Errorf("key not exists")
}
func (p TemplateFunc) Gets(pattern string) ([]KVPair, error) {
return p.Store.GetAll(pattern)
}
func (p TemplateFunc) Getv(key string, v ...string) (string, error) {
if v, ok := p.Store.GetValue(key, v...); ok {
return v, nil
}
return "", fmt.Errorf("key not exists")
}
func (p TemplateFunc) Getvs(pattern string) ([]string, error) {
return p.Store.GetAllValues(pattern)
}
// ----------------------------------------------------------------------------
// Crypt func
// ----------------------------------------------------------------------------
func (p TemplateFunc) Cget(key string) (KVPair, error) {
if len(p.PGPPrivateKey) == 0 {
return KVPair{}, fmt.Errorf("PGPPrivateKey is empty")
}
kv, err := p.FuncMap["get"].(func(string) (KVPair, error))(key)
if err != nil {
return KVPair{}, err
}
var b []byte
b, err = secconfDecode([]byte(kv.Value), bytes.NewBuffer(p.PGPPrivateKey))
if err != nil {
return KVPair{}, err
}
kv.Value = string(b)
return kv, nil
}
func (p TemplateFunc) Cgets(pattern string) ([]KVPair, error) {
if len(p.PGPPrivateKey) == 0 {
return nil, fmt.Errorf("PGPPrivateKey is empty")
}
kvs, err := p.FuncMap["gets"].(func(string) ([]KVPair, error))(pattern)
if err != nil {
return nil, err
}
for i := range kvs {
b, err := secconfDecode([]byte(kvs[i].Value), bytes.NewBuffer(p.PGPPrivateKey))
if err != nil {
return nil, err
}
kvs[i].Value = string(b)
}
return kvs, nil
}
func (p TemplateFunc) Cgetv(key string) (string, error) {
if len(p.PGPPrivateKey) == 0 {
return "", fmt.Errorf("PGPPrivateKey is empty")
}
v, err := p.FuncMap["getv"].(func(string, ...string) (string, error))(key)
if err != nil {
return "", err
}
var b []byte
b, err = secconfDecode([]byte(v), bytes.NewBuffer(p.PGPPrivateKey))
if err != nil {
return "", err
}
return string(b), err
}
func (p TemplateFunc) Cgetvs(pattern string) ([]string, error) {
if len(p.PGPPrivateKey) == 0 {
return nil, fmt.Errorf("PGPPrivateKey is empty")
}
vs, err := p.FuncMap["getvs"].(func(string) ([]string, error))(pattern)
if err != nil {
return nil, err
}
for i := range vs {
b, err := secconfDecode([]byte(vs[i]), bytes.NewBuffer(p.PGPPrivateKey))
if err != nil {
return nil, err
}
vs[i] = string(b)
}
return vs, nil
}
// ----------------------------------------------------------------------------
// util func
// ----------------------------------------------------------------------------
func (_ TemplateFunc) Base(path string) string {
return pathpkg.Base(path)
}
func (_ TemplateFunc) Split(s, sep string) []string {
return strings.Split(s, sep)
}
func (_ TemplateFunc) Json(data string) (map[string]interface{}, error) {
var ret map[string]interface{}
err := json.Unmarshal([]byte(data), &ret)
return ret, err
}
func (_ TemplateFunc) JsonArray(data string) ([]interface{}, error) {
var ret []interface{}
err := json.Unmarshal([]byte(data), &ret)
return ret, err
}
func (_ TemplateFunc) Dir(path string) string {
return pathpkg.Dir(path)
}
// Map creates a key-value map of string -> interface{}
// The i'th is the key and the i+1 is the value
func (_ TemplateFunc) Map(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
}
// 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 (_ TemplateFunc) Getenv(key string, defaultValue ...string) string {
if v := os.Getenv(key); v != "" {
return v
}
if len(defaultValue) > 0 {
return defaultValue[0]
}
return ""
}
func (_ TemplateFunc) Join(a []string, sep string) string {
return strings.Join(a, sep)
}
func (_ TemplateFunc) Datetime() time.Time {
return time.Now()
}
func (_ TemplateFunc) ToUpper(s string) string {
return strings.ToUpper(s)
}
func (_ TemplateFunc) ToLower(s string) string {
return strings.ToLower(s)
}
func (_ TemplateFunc) Contains(s, substr string) bool {
return strings.Contains(s, substr)
}
func (_ TemplateFunc) Replace(s, old, new string, n int) string {
return strings.Replace(s, old, new, n)
}
func (_ TemplateFunc) TrimSuffix(s, suffix string) string {
return strings.TrimSuffix(s, suffix)
}
func (_ TemplateFunc) 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
}
func (_ TemplateFunc) LookupSRV(service, proto, name string) []*net.SRV {
_, s, err := net.LookupSRV(service, proto, name)
if err != nil {
return nil
}
sort.Slice(s, func(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
})
return s
}
func (_ TemplateFunc) FileExists(filepath string) bool {
_, err := os.Stat(filepath)
return err == nil
}
func (_ TemplateFunc) Base64Encode(data string) string {
return base64.StdEncoding.EncodeToString([]byte(data))
}
func (_ TemplateFunc) Base64Decode(data string) (string, error) {
s, err := base64.StdEncoding.DecodeString(data)
return string(s), err
}
func (_ TemplateFunc) ParseBool(s string) (bool, error) {
return strconv.ParseBool(s)
}
// reverse returns the array in reversed order
// works with []string and []KVPair
func (_ TemplateFunc) 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 []KVPair:
v := values.([]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
}
func (_ TemplateFunc) SortKVByLength(values []KVPair) []KVPair {
sort.Slice(values, func(i, j int) bool {
return len(values[i].Key) < len(values[j].Key)
})
return values
}
func (_ TemplateFunc) SortByLength(values []string) []string {
sort.Slice(values, func(i, j int) bool {
return len(values[i]) < len(values[j])
})
return values
}
func (_ TemplateFunc) Add(a, b int) int {
return a + b
}
func (_ TemplateFunc) Sub(a, b int) int {
return a - b
}
func (_ TemplateFunc) Div(a, b int) int {
return a / b
}
func (_ TemplateFunc) Mod(a, b int) int {
return a % b
}
func (_ TemplateFunc) Mul(a, b int) int {
return a * b
}
// 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 (_ TemplateFunc) Seq(first, last int) []int {
var arr []int
for i := first; i <= last; i++ {
arr = append(arr, i)
}
return arr
}
func (_ TemplateFunc) Atoi(s string) (int, error) {
return strconv.Atoi(s)
}
// ----------------------------------------------------------------------------
// END
// ----------------------------------------------------------------------------