aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/docker/docker-credential-helpers/osxkeychain/url_non_go18.go
blob: bdf9b7b00653c290f0bff8ef633ba8ccaf9288ad (plain)
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
//+build !go1.8

package osxkeychain

import (
	"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(":"):]
}