let utils.ParseHost return err when errors happen
This commit is contained in:
parent
99f1675566
commit
e81da876df
3 changed files with 19 additions and 12 deletions
|
@ -46,7 +46,12 @@ func main() {
|
|||
flHosts = flHosts[1:] //trick to display a nice default value in the usage
|
||||
}
|
||||
for i, flHost := range flHosts {
|
||||
flHosts[i] = utils.ParseHost(docker.DEFAULTHTTPHOST, docker.DEFAULTHTTPPORT, flHost)
|
||||
host, err := utils.ParseHost(docker.DEFAULTHTTPHOST, docker.DEFAULTHTTPPORT, flHost)
|
||||
if err == nil {
|
||||
flHosts[i] = host
|
||||
} else {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
if *bridgeName != "" {
|
||||
|
|
|
@ -10,7 +10,6 @@ import (
|
|||
"index/suffixarray"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
@ -818,17 +817,17 @@ func StripComments(input []byte, commentMarker []byte) []byte {
|
|||
return output
|
||||
}
|
||||
|
||||
func ParseHost(host string, port int, addr string) string {
|
||||
func ParseHost(host string, port int, addr string) (string, error) {
|
||||
var proto string
|
||||
switch {
|
||||
case strings.HasPrefix(addr, "unix://"):
|
||||
return addr
|
||||
return addr, nil
|
||||
case strings.HasPrefix(addr, "tcp://"):
|
||||
proto = "tcp"
|
||||
addr = strings.TrimPrefix(addr, "tcp://")
|
||||
default:
|
||||
if strings.Contains(addr, "://") {
|
||||
log.Fatal("Invalid bind address proto")
|
||||
return "", fmt.Errorf("Invalid bind address protocol: %s", addr)
|
||||
}
|
||||
proto = "tcp"
|
||||
}
|
||||
|
@ -836,7 +835,7 @@ func ParseHost(host string, port int, addr string) string {
|
|||
if strings.Contains(addr, ":") {
|
||||
hostParts := strings.Split(addr, ":")
|
||||
if len(hostParts) != 2 {
|
||||
log.Fatal("Invalid bind address format.")
|
||||
return "", fmt.Errorf("Invalid bind address format: %s", addr)
|
||||
}
|
||||
if hostParts[0] != "" {
|
||||
host = hostParts[0]
|
||||
|
@ -847,7 +846,7 @@ func ParseHost(host string, port int, addr string) string {
|
|||
} else {
|
||||
host = addr
|
||||
}
|
||||
return fmt.Sprintf("%s://%s:%d", proto, host, port)
|
||||
return fmt.Sprintf("%s://%s:%d", proto, host, port), nil
|
||||
}
|
||||
|
||||
func GetReleaseVersion() string {
|
||||
|
|
|
@ -266,21 +266,24 @@ func TestHumanSize(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestParseHost(t *testing.T) {
|
||||
if addr := ParseHost("127.0.0.1", 4243, "0.0.0.0"); addr != "tcp://0.0.0.0:4243" {
|
||||
if addr, err := ParseHost("127.0.0.1", 4243, "0.0.0.0"); err != nil || addr != "tcp://0.0.0.0:4243" {
|
||||
t.Errorf("0.0.0.0 -> expected tcp://0.0.0.0:4243, got %s", addr)
|
||||
}
|
||||
if addr := ParseHost("127.0.0.1", 4243, "0.0.0.1:5555"); addr != "tcp://0.0.0.1:5555" {
|
||||
if addr, err := ParseHost("127.0.0.1", 4243, "0.0.0.1:5555"); err != nil || addr != "tcp://0.0.0.1:5555" {
|
||||
t.Errorf("0.0.0.1:5555 -> expected tcp://0.0.0.1:5555, got %s", addr)
|
||||
}
|
||||
if addr := ParseHost("127.0.0.1", 4243, ":6666"); addr != "tcp://127.0.0.1:6666" {
|
||||
if addr, err := ParseHost("127.0.0.1", 4243, ":6666"); err != nil || addr != "tcp://127.0.0.1:6666" {
|
||||
t.Errorf(":6666 -> expected tcp://127.0.0.1:6666, got %s", addr)
|
||||
}
|
||||
if addr := ParseHost("127.0.0.1", 4243, "tcp://:7777"); addr != "tcp://127.0.0.1:7777" {
|
||||
if addr, err := ParseHost("127.0.0.1", 4243, "tcp://:7777"); err != nil || addr != "tcp://127.0.0.1:7777" {
|
||||
t.Errorf("tcp://:7777 -> expected tcp://127.0.0.1:7777, got %s", addr)
|
||||
}
|
||||
if addr := ParseHost("127.0.0.1", 4243, "unix:///var/run/docker.sock"); addr != "unix:///var/run/docker.sock" {
|
||||
if addr, err := ParseHost("127.0.0.1", 4243, "unix:///var/run/docker.sock"); err != nil || addr != "unix:///var/run/docker.sock" {
|
||||
t.Errorf("unix:///var/run/docker.sock -> expected unix:///var/run/docker.sock, got %s", addr)
|
||||
}
|
||||
if addr, err := ParseHost("127.0.0.1", 4243, "udp://127.0.0.1"); err == nil {
|
||||
t.Errorf("udp protocol address expected error return, but err == nil. Got %s", addr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseRepositoryTag(t *testing.T) {
|
||||
|
|
Loading…
Reference in a new issue