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:
parent
f47ea48cfe
commit
32fb3913b5
1 changed files with 47 additions and 51 deletions
|
@ -7,63 +7,59 @@ import (
|
|||
)
|
||||
|
||||
// FIXME: Change this not to receive default value as parameter
|
||||
func ParseHost(defaultHost string, defaultUnix, addr string) (string, error) {
|
||||
var (
|
||||
proto string
|
||||
host string
|
||||
port int
|
||||
)
|
||||
func ParseHost(defaultTCPAddr, defaultUnixAddr, addr string) (string, error) {
|
||||
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 == "" {
|
||||
addr = defaultUnix
|
||||
addr = fmt.Sprintf("unix://%s", defaultUnixAddr)
|
||||
}
|
||||
case strings.HasPrefix(addr, "tcp://"):
|
||||
proto = "tcp"
|
||||
addr = strings.TrimPrefix(addr, "tcp://")
|
||||
case strings.HasPrefix(addr, "fd://"):
|
||||
return addr, nil
|
||||
case addr == "":
|
||||
proto = "unix"
|
||||
addr = defaultUnix
|
||||
default:
|
||||
if strings.Contains(addr, "://") {
|
||||
return "", fmt.Errorf("Invalid bind address protocol: %s", addr)
|
||||
}
|
||||
proto = "tcp"
|
||||
addrParts := strings.Split(addr, "://")
|
||||
if len(addrParts) == 1 {
|
||||
addrParts = []string{"tcp", addrParts[0]}
|
||||
}
|
||||
|
||||
switch addrParts[0] {
|
||||
case "tcp":
|
||||
return ParseTCPAddr(addrParts[1], defaultTCPAddr)
|
||||
case "unix":
|
||||
return ParseUnixAddr(addrParts[1], defaultUnixAddr)
|
||||
case "fd":
|
||||
return addr, nil
|
||||
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, ":")
|
||||
if len(hostParts) != 2 {
|
||||
return "", fmt.Errorf("Invalid bind address format: %s", addr)
|
||||
}
|
||||
if hostParts[0] != "" {
|
||||
host = hostParts[0]
|
||||
} else {
|
||||
host = defaultHost
|
||||
host := hostParts[0]
|
||||
if host == "" {
|
||||
host = defaultAddr
|
||||
}
|
||||
|
||||
if p, err := strconv.Atoi(hostParts[1]); err == nil && p != 0 {
|
||||
port = p
|
||||
} else {
|
||||
p, err := strconv.Atoi(hostParts[1])
|
||||
if err != nil && p == 0 {
|
||||
return "", fmt.Errorf("Invalid bind address format: %s", addr)
|
||||
}
|
||||
|
||||
} 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
|
||||
return fmt.Sprintf("tcp://%s:%d", host, p), nil
|
||||
}
|
||||
|
||||
// Get a repos name and returns the right reposName + tag
|
||||
|
|
Loading…
Reference in a new issue