From 99f16755661de79cac1ccda78e3e96552f6a4491 Mon Sep 17 00:00:00 2001 From: Yang Bai Date: Wed, 23 Oct 2013 18:29:35 +0800 Subject: [PATCH] refactor utils.ParseHost with switch-case, so we can add other proto support easily --- utils/utils.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/utils/utils.go b/utils/utils.go index b0327dd40c..17bb0e377b 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -819,17 +819,24 @@ func StripComments(input []byte, commentMarker []byte) []byte { } 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 - } - if strings.HasPrefix(addr, "tcp://") { + case strings.HasPrefix(addr, "tcp://"): + proto = "tcp" addr = strings.TrimPrefix(addr, "tcp://") + default: + if strings.Contains(addr, "://") { + log.Fatal("Invalid bind address proto") + } + proto = "tcp" } + if strings.Contains(addr, ":") { hostParts := strings.Split(addr, ":") if len(hostParts) != 2 { log.Fatal("Invalid bind address format.") - os.Exit(-1) } if hostParts[0] != "" { host = hostParts[0] @@ -840,7 +847,7 @@ func ParseHost(host string, port int, addr string) string { } else { host = addr } - return fmt.Sprintf("tcp://%s:%d", host, port) + return fmt.Sprintf("%s://%s:%d", proto, host, port) } func GetReleaseVersion() string {