forked from docker/docker-credential-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
51 lines (43 loc) · 1.27 KB
/
parse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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()
}