mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
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
|
flHosts = flHosts[1:] //trick to display a nice default value in the usage
|
||||||
}
|
}
|
||||||
for i, flHost := range flHosts {
|
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 != "" {
|
if *bridgeName != "" {
|
||||||
|
|
|
@ -10,7 +10,6 @@ import (
|
||||||
"index/suffixarray"
|
"index/suffixarray"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
@ -818,17 +817,17 @@ func StripComments(input []byte, commentMarker []byte) []byte {
|
||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseHost(host string, port int, addr string) string {
|
func ParseHost(host string, port int, addr string) (string, error) {
|
||||||
var proto string
|
var proto string
|
||||||
switch {
|
switch {
|
||||||
case strings.HasPrefix(addr, "unix://"):
|
case strings.HasPrefix(addr, "unix://"):
|
||||||
return addr
|
return addr, nil
|
||||||
case strings.HasPrefix(addr, "tcp://"):
|
case strings.HasPrefix(addr, "tcp://"):
|
||||||
proto = "tcp"
|
proto = "tcp"
|
||||||
addr = strings.TrimPrefix(addr, "tcp://")
|
addr = strings.TrimPrefix(addr, "tcp://")
|
||||||
default:
|
default:
|
||||||
if strings.Contains(addr, "://") {
|
if strings.Contains(addr, "://") {
|
||||||
log.Fatal("Invalid bind address proto")
|
return "", fmt.Errorf("Invalid bind address protocol: %s", addr)
|
||||||
}
|
}
|
||||||
proto = "tcp"
|
proto = "tcp"
|
||||||
}
|
}
|
||||||
|
@ -836,7 +835,7 @@ func ParseHost(host string, port int, addr string) string {
|
||||||
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.")
|
return "", fmt.Errorf("Invalid bind address format: %s", addr)
|
||||||
}
|
}
|
||||||
if hostParts[0] != "" {
|
if hostParts[0] != "" {
|
||||||
host = hostParts[0]
|
host = hostParts[0]
|
||||||
|
@ -847,7 +846,7 @@ func ParseHost(host string, port int, addr string) string {
|
||||||
} else {
|
} else {
|
||||||
host = addr
|
host = addr
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("%s://%s:%d", proto, host, port)
|
return fmt.Sprintf("%s://%s:%d", proto, host, port), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetReleaseVersion() string {
|
func GetReleaseVersion() string {
|
||||||
|
|
|
@ -266,21 +266,24 @@ func TestHumanSize(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseHost(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)
|
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)
|
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)
|
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)
|
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)
|
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) {
|
func TestParseRepositoryTag(t *testing.T) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue