1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

refactor utils.ParseHost with switch-case, so we can add other proto support easily

This commit is contained in:
Yang Bai 2013-10-23 18:29:35 +08:00
parent b0515a7016
commit 99f1675566

View file

@ -819,17 +819,24 @@ func StripComments(input []byte, commentMarker []byte) []byte {
} }
func ParseHost(host string, port int, addr string) string { func ParseHost(host string, port int, addr string) string {
if strings.HasPrefix(addr, "unix://") { var proto string
switch {
case strings.HasPrefix(addr, "unix://"):
return addr return addr
} case strings.HasPrefix(addr, "tcp://"):
if strings.HasPrefix(addr, "tcp://") { proto = "tcp"
addr = strings.TrimPrefix(addr, "tcp://") addr = strings.TrimPrefix(addr, "tcp://")
default:
if strings.Contains(addr, "://") {
log.Fatal("Invalid bind address proto")
}
proto = "tcp"
} }
if strings.Contains(addr, ":") { if strings.Contains(addr, ":") {
hostParts := strings.Split(addr, ":") hostParts := strings.Split(addr, ":")
if len(hostParts) != 2 { if len(hostParts) != 2 {
log.Fatal("Invalid bind address format.") log.Fatal("Invalid bind address format.")
os.Exit(-1)
} }
if hostParts[0] != "" { if hostParts[0] != "" {
host = hostParts[0] host = hostParts[0]
@ -840,7 +847,7 @@ func ParseHost(host string, port int, addr string) string {
} else { } else {
host = addr host = addr
} }
return fmt.Sprintf("tcp://%s:%d", host, port) return fmt.Sprintf("%s://%s:%d", proto, host, port)
} }
func GetReleaseVersion() string { func GetReleaseVersion() string {