mirror of https://github.com/docker/cli.git
cli/config/credentials: ConvertToHostname: handle IP-addresses
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
94e9aa6891
commit
8b0a7b025d
|
@ -1,6 +1,7 @@
|
||||||
package credentials
|
package credentials
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -77,7 +78,7 @@ func ConvertToHostname(maybeURL string) string {
|
||||||
if u.Port() == "" {
|
if u.Port() == "" {
|
||||||
return u.Hostname()
|
return u.Hostname()
|
||||||
}
|
}
|
||||||
return u.Hostname() + ":" + u.Port()
|
return net.JoinHostPort(u.Hostname(), u.Port())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
hostName, _, _ := strings.Cut(stripped, "/")
|
hostName, _, _ := strings.Cut(stripped, "/")
|
||||||
|
|
|
@ -137,6 +137,19 @@ func TestFileStoreErase(t *testing.T) {
|
||||||
|
|
||||||
func TestConvertToHostname(t *testing.T) {
|
func TestConvertToHostname(t *testing.T) {
|
||||||
tests := []struct{ input, expected string }{
|
tests := []struct{ input, expected string }{
|
||||||
|
{
|
||||||
|
input: "127.0.0.1",
|
||||||
|
expected: "127.0.0.1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "::1",
|
||||||
|
expected: "::1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// FIXME(thaJeztah): this should be normalized to "::1" if there's no port (or vice-versa, as long as we're consistent)
|
||||||
|
input: "[::1]",
|
||||||
|
expected: "[::1]",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
input: "example.com",
|
input: "example.com",
|
||||||
expected: "example.com",
|
expected: "example.com",
|
||||||
|
@ -168,10 +181,35 @@ func TestConvertToHostname(t *testing.T) {
|
||||||
expected: "example.com",
|
expected: "example.com",
|
||||||
},
|
},
|
||||||
// should support non-standard port in registry url
|
// should support non-standard port in registry url
|
||||||
|
{
|
||||||
|
input: "127.0.0.1:6556",
|
||||||
|
expected: "127.0.0.1:6556",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// FIXME(thaJeztah): this should be normalized to "[::1]:6556"
|
||||||
|
input: "::1:6556",
|
||||||
|
expected: "::1:6556",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "[::1]:6556",
|
||||||
|
expected: "[::1]:6556",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
input: "example.com:6555",
|
input: "example.com:6555",
|
||||||
expected: "example.com:6555",
|
expected: "example.com:6555",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
input: "https://127.0.0.1:6555/v2/",
|
||||||
|
expected: "127.0.0.1:6555",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "https://::1:6555/v2/",
|
||||||
|
expected: "[::1]:6555",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "https://[::1]:6555/v2/",
|
||||||
|
expected: "[::1]:6555",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
input: "http://example.com:6555",
|
input: "http://example.com:6555",
|
||||||
expected: "example.com:6555",
|
expected: "example.com:6555",
|
||||||
|
|
Loading…
Reference in New Issue