mirror of
https://github.com/docker/docker-credential-helpers.git
synced 2026-06-13 16:01:28 +05:30
a3c1b5b757
Signed-off-by: Emily Casey <ecasey@pivotal.io>
42 lines
749 B
Go
42 lines
749 B
Go
//+build !go1.8
|
|
|
|
package registryurl
|
|
|
|
import (
|
|
url "net/url"
|
|
"strings"
|
|
)
|
|
|
|
func GetHostname(u *url.URL) string {
|
|
return stripPort(u.Host)
|
|
}
|
|
|
|
func GetPort(u *url.URL) string {
|
|
return portOnly(u.Host)
|
|
}
|
|
|
|
func stripPort(hostport string) string {
|
|
colon := strings.IndexByte(hostport, ':')
|
|
if colon == -1 {
|
|
return hostport
|
|
}
|
|
if i := strings.IndexByte(hostport, ']'); i != -1 {
|
|
return strings.TrimPrefix(hostport[:i], "[")
|
|
}
|
|
return hostport[:colon]
|
|
}
|
|
|
|
func portOnly(hostport string) string {
|
|
colon := strings.IndexByte(hostport, ':')
|
|
if colon == -1 {
|
|
return ""
|
|
}
|
|
if i := strings.Index(hostport, "]:"); i != -1 {
|
|
return hostport[i+len("]:"):]
|
|
}
|
|
if strings.Contains(hostport, "]") {
|
|
return ""
|
|
}
|
|
return hostport[colon+len(":"):]
|
|
}
|