cli/internal/ghinstance/host.go
Babak K. Shandiz 139c2c4f9a
refactor: replace backport with strings.CutSuffix
The `cutSuffix` function was added to backport the functionality of
`strings.CutSuffix` from Go 1.20. Now that we're using Go 1.25, we can
safely replace our backport with the standard library function. Our
backport was an intact copy/paste of the stdlib implementation, so this
change does not alter any behavior.

Signed-off-by: Babak K. Shandiz <babakks@github.com>
2025-10-31 12:21:02 +00:00

98 lines
2.5 KiB
Go

package ghinstance
import (
"errors"
"fmt"
"strings"
ghauth "github.com/cli/go-gh/v2/pkg/auth"
)
// DefaultHostname is the domain name of the default GitHub instance.
const defaultHostname = "github.com"
// Localhost is the domain name of a local GitHub instance.
const localhost = "github.localhost"
// TenancyHost is the domain name of a tenancy GitHub instance.
const tenancyHost = "ghe.com"
// Default returns the host name of the default GitHub instance.
func Default() string {
return defaultHostname
}
// TenantName extracts the tenant name from tenancy host name and
// reports whether it found the tenant name.
func TenantName(h string) (string, bool) {
normalizedHostName := ghauth.NormalizeHostname(h)
return strings.CutSuffix(normalizedHostName, "."+tenancyHost)
}
func isGarage(h string) bool {
return strings.EqualFold(h, "garage.github.com")
}
func HostnameValidator(hostname string) error {
if len(strings.TrimSpace(hostname)) < 1 {
return errors.New("a value is required")
}
if strings.ContainsRune(hostname, '/') || strings.ContainsRune(hostname, ':') {
return errors.New("invalid hostname")
}
return nil
}
func GraphQLEndpoint(hostname string) string {
if isGarage(hostname) {
return fmt.Sprintf("https://%s/api/graphql", hostname)
}
if ghauth.IsEnterprise(hostname) {
return fmt.Sprintf("https://%s/api/graphql", hostname)
}
if strings.EqualFold(hostname, localhost) {
return fmt.Sprintf("http://api.%s/graphql", hostname)
}
return fmt.Sprintf("https://api.%s/graphql", hostname)
}
func RESTPrefix(hostname string) string {
if isGarage(hostname) {
return fmt.Sprintf("https://%s/api/v3/", hostname)
}
if ghauth.IsEnterprise(hostname) {
return fmt.Sprintf("https://%s/api/v3/", hostname)
}
if strings.EqualFold(hostname, localhost) {
return fmt.Sprintf("http://api.%s/", hostname)
}
return fmt.Sprintf("https://api.%s/", hostname)
}
func GistPrefix(hostname string) string {
prefix := "https://"
if strings.EqualFold(hostname, localhost) {
prefix = "http://"
}
return prefix + GistHost(hostname)
}
func GistHost(hostname string) string {
if isGarage(hostname) {
return fmt.Sprintf("%s/gist/", hostname)
}
if ghauth.IsEnterprise(hostname) {
return fmt.Sprintf("%s/gist/", hostname)
}
if strings.EqualFold(hostname, localhost) {
return fmt.Sprintf("%s/gist/", hostname)
}
return fmt.Sprintf("gist.%s/", hostname)
}
func HostPrefix(hostname string) string {
if strings.EqualFold(hostname, localhost) {
return fmt.Sprintf("http://%s/", hostname)
}
return fmt.Sprintf("https://%s/", hostname)
}