cli/internal/ghinstance/host.go
Kynan Ware b8ee5c5eba Add godoc comments to exported symbols in remaining packages
Add documentation comments to exported symbols across all remaining
smaller packages including cmd/gen-docs, context, internal/browser,
internal/gh, internal/ghcmd, internal/ghinstance, internal/ghrepo,
internal/keyring, internal/run, internal/safepaths, internal/tableprinter,
internal/text, internal/update, pkg/cmd/accessibility, pkg/cmd/actions,
pkg/cmd/alias, pkg/cmd/api, pkg/cmd/browse, pkg/cmd/cache,
pkg/cmd/completion, pkg/cmd/copilot, pkg/cmd/factory, pkg/cmd/gpg-key,
pkg/cmd/label, pkg/cmd/licenses, pkg/cmd/org, pkg/cmd/preview,
pkg/cmd/ssh-key, pkg/cmd/version, pkg/extensions, pkg/jsoncolor,
pkg/markdown, pkg/option, pkg/set, pkg/ssh, pkg/surveyext, test,
internal/authflow, and utils.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-04 16:14:20 -07:00

104 lines
2.9 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")
}
// HostnameValidator returns a function that validates GitHub hostnames.
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
}
// GraphQLEndpoint returns the GraphQL API endpoint for the given host.
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)
}
// RESTPrefix returns the REST API URL prefix for the given host.
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)
}
// GistPrefix returns the Gist URL prefix for the given host.
func GistPrefix(hostname string) string {
prefix := "https://"
if strings.EqualFold(hostname, localhost) {
prefix = "http://"
}
return prefix + GistHost(hostname)
}
// GistHost returns the Gist host for the given GitHub host.
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)
}
// HostPrefix returns the URL prefix for the given host.
func HostPrefix(hostname string) string {
if strings.EqualFold(hostname, localhost) {
return fmt.Sprintf("http://%s/", hostname)
}
return fmt.Sprintf("https://%s/", hostname)
}