-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost.go
64 lines (53 loc) · 1.57 KB
/
host.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 ghinstance
import (
"fmt"
"strings"
)
const defaultHostname = "github.com"
var hostnameOverride string
// Default returns the host name of the default GitHub instance
func Default() string {
return defaultHostname
}
// OverridableDefault is like Default, except it is overridable by the GH_HOST environment variable
func OverridableDefault() string {
if hostnameOverride != "" {
return hostnameOverride
}
return defaultHostname
}
// OverrideDefault overrides the value returned from OverridableDefault. This should only ever be
// called from the main runtime path, not tests.
func OverrideDefault(newhost string) {
hostnameOverride = newhost
}
// IsEnterprise reports whether a non-normalized host name looks like a GHE instance
func IsEnterprise(h string) bool {
return NormalizeHostname(h) != defaultHostname
}
// NormalizeHostname returns the canonical host name of a GitHub instance
func NormalizeHostname(h string) string {
hostname := strings.ToLower(h)
if strings.HasSuffix(hostname, "."+defaultHostname) {
return defaultHostname
}
return hostname
}
func GraphQLEndpoint(hostname string) string {
if IsEnterprise(hostname) {
return fmt.Sprintf("https://%s/api/graphql", hostname)
}
return "https://api.github.com/graphql"
}
func RESTPrefix(hostname string) string {
if IsEnterprise(hostname) {
return fmt.Sprintf("https://%s/api/v3/", hostname)
}
return "https://api.github.com/"
}
func GistPrefix(hostname string) string {
if IsEnterprise(hostname) {
return fmt.Sprintf("https://%s/gist/", hostname)
}
return fmt.Sprintf("https://gist.%s/", hostname)
}