Cleanup ParseHost

Current implementation is comingling things that ought not be together.
There are _some_ similarities between parsing for the different proto
types, but they are more different than alike, making the code extremely
difficult to reason about.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
This commit is contained in:
Brian Goff 2014-11-07 13:44:35 -05:00
parent f47ea48cfe
commit 32fb3913b5

View file

@ -7,63 +7,59 @@ import (
) )
// FIXME: Change this not to receive default value as parameter // FIXME: Change this not to receive default value as parameter
func ParseHost(defaultHost string, defaultUnix, addr string) (string, error) { func ParseHost(defaultTCPAddr, defaultUnixAddr, addr string) (string, error) {
var (
proto string
host string
port int
)
addr = strings.TrimSpace(addr) addr = strings.TrimSpace(addr)
switch {
case addr == "tcp://":
return "", fmt.Errorf("Invalid bind address format: %s", addr)
case strings.HasPrefix(addr, "unix://"):
proto = "unix"
addr = strings.TrimPrefix(addr, "unix://")
if addr == "" { if addr == "" {
addr = defaultUnix addr = fmt.Sprintf("unix://%s", defaultUnixAddr)
} }
case strings.HasPrefix(addr, "tcp://"): addrParts := strings.Split(addr, "://")
proto = "tcp" if len(addrParts) == 1 {
addr = strings.TrimPrefix(addr, "tcp://") addrParts = []string{"tcp", addrParts[0]}
case strings.HasPrefix(addr, "fd://"): }
return addr, nil
case addr == "": switch addrParts[0] {
proto = "unix" case "tcp":
addr = defaultUnix return ParseTCPAddr(addrParts[1], defaultTCPAddr)
default: case "unix":
if strings.Contains(addr, "://") { return ParseUnixAddr(addrParts[1], defaultUnixAddr)
return "", fmt.Errorf("Invalid bind address protocol: %s", addr) case "fd":
} return addr, nil
proto = "tcp" default:
return "", fmt.Errorf("Invalid bind address format: %s", addr)
}
}
func ParseUnixAddr(addr string, defaultAddr string) (string, error) {
addr = strings.TrimPrefix(addr, "unix://")
if strings.Contains(addr, "://") {
return "", fmt.Errorf("Invalid proto, expected unix: %s", addr)
}
if addr == "" {
addr = defaultAddr
}
return fmt.Sprintf("unix://%s", addr), nil
}
func ParseTCPAddr(addr string, defaultAddr string) (string, error) {
addr = strings.TrimPrefix(addr, "tcp://")
if strings.Contains(addr, "://") || addr == "" {
return "", fmt.Errorf("Invalid proto, expected tcp: %s", addr)
} }
if proto != "unix" && strings.Contains(addr, ":") {
hostParts := strings.Split(addr, ":") hostParts := strings.Split(addr, ":")
if len(hostParts) != 2 { if len(hostParts) != 2 {
return "", fmt.Errorf("Invalid bind address format: %s", addr) return "", fmt.Errorf("Invalid bind address format: %s", addr)
} }
if hostParts[0] != "" { host := hostParts[0]
host = hostParts[0] if host == "" {
} else { host = defaultAddr
host = defaultHost
} }
if p, err := strconv.Atoi(hostParts[1]); err == nil && p != 0 { p, err := strconv.Atoi(hostParts[1])
port = p if err != nil && p == 0 {
} else {
return "", fmt.Errorf("Invalid bind address format: %s", addr) return "", fmt.Errorf("Invalid bind address format: %s", addr)
} }
return fmt.Sprintf("tcp://%s:%d", host, p), nil
} else if proto == "tcp" && !strings.Contains(addr, ":") {
return "", fmt.Errorf("Invalid bind address format: %s", addr)
} else {
host = addr
}
if proto == "unix" {
return fmt.Sprintf("%s://%s", proto, host), nil
}
return fmt.Sprintf("%s://%s:%d", proto, host, port), nil
} }
// Get a repos name and returns the right reposName + tag // Get a repos name and returns the right reposName + tag