MCPcopy Index your code
hub / github.com/nginx-proxy/docker-gen / parseHost

Function parseHost

internal/dockerclient/docker_cli.go:60–119  ·  view source on GitHub ↗

based off of https://github.com/dotcloud/docker/blob/2a711d16e05b69328f2636f88f8eac035477f7e4/utils/utils.go

(addr string)

Source from the content-addressed store, hash-verified

58
59// based off of https://github.com/dotcloud/docker/blob/2a711d16e05b69328f2636f88f8eac035477f7e4/utils/utils.go
60func parseHost(addr string) (string, string, error) {
61
62 var (
63 proto string
64 host string
65 port int
66 )
67 addr = strings.TrimSpace(addr)
68 switch {
69 case addr == "tcp://":
70 return "", "", fmt.Errorf("invalid bind address format: %s", addr)
71 case strings.HasPrefix(addr, "unix://"):
72 proto = "unix"
73 addr = strings.TrimPrefix(addr, "unix://")
74 if addr == "" {
75 addr = "/var/run/docker.sock"
76 }
77 case strings.HasPrefix(addr, "tcp://"):
78 proto = "tcp"
79 addr = strings.TrimPrefix(addr, "tcp://")
80 case strings.HasPrefix(addr, "fd://"):
81 return "fd", addr, nil
82 case addr == "":
83 proto = "unix"
84 addr = "/var/run/docker.sock"
85 default:
86 if strings.Contains(addr, "://") {
87 return "", "", fmt.Errorf("invalid bind address protocol: %s", addr)
88 }
89 proto = "tcp"
90 }
91
92 if proto != "unix" && strings.Contains(addr, ":") {
93 hostParts := strings.Split(addr, ":")
94 if len(hostParts) != 2 {
95 return "", "", fmt.Errorf("invalid bind address format: %s", addr)
96 }
97 if hostParts[0] != "" {
98 host = hostParts[0]
99 } else {
100 host = "127.0.0.1"
101 }
102
103 if p, err := strconv.Atoi(hostParts[1]); err == nil && p != 0 {
104 port = p
105 } else {
106 return "", "", fmt.Errorf("invalid bind address format: %s", addr)
107 }
108
109 } else if proto == "tcp" && !strings.Contains(addr, ":") {
110 return "", "", fmt.Errorf("invalid bind address format: %s", addr)
111 } else {
112 host = addr
113 }
114 if proto == "unix" {
115 return proto, host, nil
116
117 }

Callers 8

GetEndpointFunction · 0.85
TestParseHostUnixFunction · 0.85
TestParseHostUnixDefaultFunction · 0.85
TestParseHostTCPFunction · 0.85
TestParseHostTCPDefaultFunction · 0.85
TestParseHostSystemdFunction · 0.85
assertParseHostErrorFunction · 0.85

Calls

no outgoing calls

Tested by 7

TestParseHostUnixFunction · 0.68
TestParseHostUnixDefaultFunction · 0.68
TestParseHostTCPFunction · 0.68
TestParseHostTCPDefaultFunction · 0.68
TestParseHostSystemdFunction · 0.68
assertParseHostErrorFunction · 0.68