-
Notifications
You must be signed in to change notification settings - Fork 8
/
tpl_func.go
132 lines (116 loc) · 2.88 KB
/
tpl_func.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
package main
import (
"encoding/json"
"errors"
"fmt"
"net"
"os"
"path"
"sort"
"strings"
"time"
)
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["lookupIP"] = LookupIP
m["lookupSRV"] = LookupSRV
m["fileExists"] = isFileExist
return m
}
func addFuncs(out, in map[string]interface{}) {
for name, fn := range in {
out[name] = fn
}
}
// 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 isFileExist(fpath string) bool {
if _, err := os.Stat(fpath); os.IsNotExist(err) {
return false
}
return true
}