-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
64 lines (57 loc) · 1.29 KB
/
util.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
package main
import (
"log"
"net"
"os"
"strings"
)
func fileExists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
// GetOutboundIP Gets preferred outbound ip of this machine
func GetOutboundIP() string {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
localAddr := conn.LocalAddr().String()
idx := strings.LastIndex(localAddr, ":")
return localAddr[0:idx]
}
func singleJoiningSlash(a, b string) string {
aslash := strings.HasSuffix(a, "/")
bslash := strings.HasPrefix(b, "/")
switch {
case aslash && bslash:
return a + b[1:]
case !aslash && !bslash:
return a + "/" + b
}
return a + b
}
// combineMaps recursively combines n `map[string]interface{}` objects.
// Maps passed later in the list overwrite earlier ones.
func combineMaps(cfgs ...map[string]interface{}) map[string]interface{} {
combined := map[string]interface{}{}
for _, cfg := range cfgs {
for key, val := range cfg {
if _, ok := combined[key]; ok {
switch v := val.(type) {
default:
combined[key] = val
case map[string]interface{}:
combined[key] = combineMaps(combined[key].(map[string]interface{}), v)
}
} else {
combined[key] = val
}
}
}
return combined
}