forked from mantl/mesos-consul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
55 lines (43 loc) · 925 Bytes
/
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
package mesos
import (
"log"
"net"
"regexp"
"strconv"
"strings"
)
func cleanName(name string) string {
reg, err := regexp.Compile("[^\\w-.\\.]")
if err != nil {
log.Print("[WARN] ", err)
return name
}
s := reg.ReplaceAllString(name, "")
return strings.ToLower(strings.Replace(s, "_", "", -1))
}
// The PID has a specific format:
// type@host:port
func parsePID(pid string) (string, string) {
host := strings.Split(strings.Split(pid, ":")[0], "@")[1]
port := strings.Split(pid, ":")[1]
return host, port
}
func leaderIP(leader string) string {
host := strings.Split(leader, "@")[1]
host = strings.Split(host, ":")[0]
return host
}
func toIP(host string) string {
ip, err := net.LookupIP(host)
if err != nil {
return host
}
return ip[0].String()
}
func toPort(p string) int {
ps, err := strconv.Atoi(p)
if err != nil {
log.Printf("[ERROR] Invalid port number: %s", p)
}
return ps
}