github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/internal/ghinstance/host.go (about)

     1  package ghinstance
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  )
     8  
     9  const defaultHostname = "github.com"
    10  
    11  // localhost is the domain name of a local GitHub instance
    12  const localhost = "github.localhost"
    13  
    14  // Default returns the host name of the default GitHub instance
    15  func Default() string {
    16  	return defaultHostname
    17  }
    18  
    19  // IsEnterprise reports whether a non-normalized host name looks like a GHE instance
    20  func IsEnterprise(h string) bool {
    21  	normalizedHostName := NormalizeHostname(h)
    22  	return normalizedHostName != defaultHostname && normalizedHostName != localhost
    23  }
    24  
    25  func isGarage(h string) bool {
    26  	return strings.EqualFold(h, "garage.github.com")
    27  }
    28  
    29  // NormalizeHostname returns the canonical host name of a GitHub instance
    30  func NormalizeHostname(h string) string {
    31  	hostname := strings.ToLower(h)
    32  	if strings.HasSuffix(hostname, "."+defaultHostname) {
    33  		return defaultHostname
    34  	}
    35  
    36  	if strings.HasSuffix(hostname, "."+localhost) {
    37  		return localhost
    38  	}
    39  
    40  	return hostname
    41  }
    42  
    43  func HostnameValidator(hostname string) error {
    44  	if len(strings.TrimSpace(hostname)) < 1 {
    45  		return errors.New("a value is required")
    46  	}
    47  	if strings.ContainsRune(hostname, '/') || strings.ContainsRune(hostname, ':') {
    48  		return errors.New("invalid hostname")
    49  	}
    50  	return nil
    51  }
    52  
    53  func GraphQLEndpoint(hostname string) string {
    54  	if isGarage(hostname) {
    55  		return fmt.Sprintf("https://%s/api/graphql", hostname)
    56  	}
    57  	if IsEnterprise(hostname) {
    58  		return fmt.Sprintf("https://%s/api/graphql", hostname)
    59  	}
    60  	if strings.EqualFold(hostname, localhost) {
    61  		return fmt.Sprintf("http://api.%s/graphql", hostname)
    62  	}
    63  	return fmt.Sprintf("https://api.%s/graphql", hostname)
    64  }
    65  
    66  func RESTPrefix(hostname string) string {
    67  	if isGarage(hostname) {
    68  		return fmt.Sprintf("https://%s/api/v3/", hostname)
    69  	}
    70  	if IsEnterprise(hostname) {
    71  		return fmt.Sprintf("https://%s/api/v3/", hostname)
    72  	}
    73  	if strings.EqualFold(hostname, localhost) {
    74  		return fmt.Sprintf("http://api.%s/", hostname)
    75  	}
    76  	return fmt.Sprintf("https://api.%s/", hostname)
    77  }
    78  
    79  func GistPrefix(hostname string) string {
    80  	prefix := "https://"
    81  
    82  	if strings.EqualFold(hostname, localhost) {
    83  		prefix = "http://"
    84  	}
    85  
    86  	return prefix + GistHost(hostname)
    87  }
    88  
    89  func GistHost(hostname string) string {
    90  	if isGarage(hostname) {
    91  		return fmt.Sprintf("%s/gist/", hostname)
    92  	}
    93  	if IsEnterprise(hostname) {
    94  		return fmt.Sprintf("%s/gist/", hostname)
    95  	}
    96  	if strings.EqualFold(hostname, localhost) {
    97  		return fmt.Sprintf("%s/gist/", hostname)
    98  	}
    99  	return fmt.Sprintf("gist.%s/", hostname)
   100  }
   101  
   102  func HostPrefix(hostname string) string {
   103  	if strings.EqualFold(hostname, localhost) {
   104  		return fmt.Sprintf("http://%s/", hostname)
   105  	}
   106  	return fmt.Sprintf("https://%s/", hostname)
   107  }