forked from mislav/hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hosts.go
59 lines (46 loc) · 953 Bytes
/
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
package github
import (
"fmt"
"net/url"
"os"
"strings"
"github.com/github/hub/git"
)
var (
GitHubHostEnv = os.Getenv("GITHUB_HOST")
)
type GitHubHosts []string
type GithubHostError struct {
url *url.URL
}
func (e *GithubHostError) Error() string {
return fmt.Sprintf("Invalid GitHub URL: %s", e.url)
}
func (h GitHubHosts) Include(host string) bool {
for _, hh := range h {
if hh == host {
return true
}
}
return false
}
func knownGitHubHosts() (hosts GitHubHosts) {
defaultHost := DefaultGitHubHost()
hosts = append(hosts, defaultHost)
hosts = append(hosts, "ssh."+GitHubHost)
ghHosts, _ := git.Config("hub.host")
for _, ghHost := range strings.Split(ghHosts, "\n") {
ghHost = strings.TrimSpace(ghHost)
if ghHosts != "" {
hosts = append(hosts, ghHost)
}
}
return
}
func DefaultGitHubHost() string {
defaultHost := GitHubHostEnv
if defaultHost == "" {
defaultHost = GitHubHost
}
return defaultHost
}