1
0
mirror of https://github.com/docker/docker-credential-helpers.git synced 2026-06-14 00:11:28 +05:30
Files
docker-credential-helpers/registryurl/parse.go
T
Sebastiaan van Stijn 70f476531f registryurl: deprecate GetHostname(), GetPort()
These were just wrappers for url.Hostname() and url.Port()

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-08-20 20:25:03 +02:00

52 lines
1.3 KiB
Go

package registryurl
import (
"errors"
"net/url"
"strings"
)
// Parse parses and validates a given serverURL to an url.URL, and
// returns an error if validation failed. Querystring parameters are
// omitted in the resulting URL, because they are not used in the helper.
//
// If serverURL does not have a valid scheme, `//` is used as scheme
// before parsing. This prevents the hostname being used as path,
// and the credentials being stored without host.
func Parse(registryURL string) (*url.URL, error) {
// Check if registryURL has a scheme, otherwise add `//` as scheme.
if !strings.Contains(registryURL, "://") && !strings.HasPrefix(registryURL, "//") {
registryURL = "//" + registryURL
}
u, err := url.Parse(registryURL)
if err != nil {
return nil, err
}
if u.Scheme != "" && u.Scheme != "https" && u.Scheme != "http" {
return nil, errors.New("unsupported scheme: " + u.Scheme)
}
if u.Hostname() == "" {
return nil, errors.New("no hostname in URL")
}
u.RawQuery = ""
return u, nil
}
// GetHostname returns the hostname of the URL
//
// Deprecated: use url.Hostname()
func GetHostname(u *url.URL) string {
return u.Hostname()
}
// GetPort returns the port number of the URL
//
// Deprecated: use url.Port()
func GetPort(u *url.URL) string {
return u.Port()
}