-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.go
84 lines (64 loc) · 1.56 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package lunar
import (
"net"
"path/filepath"
"strings"
)
func normalizeURL(url string) string {
if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
url = "http://" + url
}
return strings.TrimSuffix(url, "/")
}
// GetLocalIP gets local ip
func GetLocalIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
return ""
}
func isSupported(format string) bool {
supportedFormats := []string{".properties", ".xml", ".json", ".yml", ".yaml", ".txt"}
for _, f := range supportedFormats {
if format == f {
return true
}
}
return false
}
// GetFormat gets the format of namespace
func GetFormat(namespace string) string {
ext := filepath.Ext(namespace)
if ext == "" || !isSupported(ext) {
ext = defaultFormat
}
return strings.TrimPrefix(ext, ".")
}
// IsProperties checks if the format of namespace is properties
func IsProperties(namespace string) bool {
return GetFormat(namespace) == defaultFormat
}
func normalizeNamespace(namespace string) string {
return strings.TrimSuffix(namespace, ".properties")
}
func refineNamespaces(namespaces []string) []string {
type empty struct{}
namespaces = append(namespaces, defaultNamespace)
set := make(map[string]empty)
for _, ns := range namespaces {
set[normalizeNamespace(ns)] = empty{}
}
var result []string
for k := range set {
result = append(result, k)
}
return result
}