forked from mislav/hub
/
hosts.go
64 lines (51 loc) · 1.01 KB
/
hosts.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
52
53
54
55
56
57
58
59
60
61
62
63
64
package github
import (
"fmt"
"net/url"
"os"
"strings"
"github.com/github/hub/git"
)
var (
GitHubHostEnv = os.Getenv("GITHUB_HOST")
cachedHosts []string
)
type GithubHostError struct {
url *url.URL
}
func (e *GithubHostError) Error() string {
return fmt.Sprintf("Invalid GitHub URL: %s", e.url)
}
func knownGitHubHostsInclude(host string) bool {
for _, hh := range knownGitHubHosts() {
if hh == host {
return true
}
}
return false
}
func knownGitHubHosts() []string {
if cachedHosts != nil {
return cachedHosts
}
hosts := []string{}
defaultHost := DefaultGitHubHost()
hosts = append(hosts, defaultHost)
hosts = append(hosts, "ssh."+GitHubHost)
ghHosts, _ := git.ConfigAll("hub.host")
for _, ghHost := range ghHosts {
ghHost = strings.TrimSpace(ghHost)
if ghHost != "" {
hosts = append(hosts, ghHost)
}
}
cachedHosts = hosts
return hosts
}
func DefaultGitHubHost() string {
defaultHost := GitHubHostEnv
if defaultHost == "" {
defaultHost = GitHubHost
}
return defaultHost
}