Skip to content
Snippets Groups Projects
utils.go 880 B
package membership

import (
	"log"
	"net"
	"os"
	"strconv"
	"strings"
)

func parseHostname(id string) string {
	components := strings.Split(id, ":")
	return components[0]
}

func parseAddress(id string) string {
	components := strings.Split(id, ":")
	return components[1] + ":" + components[2]
}

func parseTimestamp(id string) int64 {
	components := strings.Split(id, ":")
	t, _ := strconv.ParseInt(components[3], 10, 64)
	return t
}

// resolve hosts' name to IP address
func getIP(hostname string) net.IP {
	ips, err := net.LookupIP(hostname)
	if err != nil {
		log.Println("Fail to resolve hostname:", err)
		os.Exit(1)
	}
	for _, ip := range ips {
		log.Println("IP Address:", ip.String())
	}
	return ips[0]
}

func getLocalIP() net.IP {
	hostname, err := os.Hostname()
	if err != nil {
		log.Println("Fail to get hostname:", err)
		os.Exit(1)
	}
	return getIP(hostname)
}