github.com/secman-team/gh-api@v1.8.2/core/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  // Default returns the host name of the default GitHub instance
    12  func Default() string {
    13  	return defaultHostname
    14  }
    15  
    16  // IsEnterprise reports whether a non-normalized host name looks like a GHE instance
    17  func IsEnterprise(h string) bool {
    18  	return NormalizeHostname(h) != defaultHostname
    19  }
    20  
    21  // NormalizeHostname returns the canonical host name of a GitHub instance
    22  func NormalizeHostname(h string) string {
    23  	hostname := strings.ToLower(h)
    24  	if strings.HasSuffix(hostname, "."+defaultHostname) {
    25  		return defaultHostname
    26  	}
    27  	return hostname
    28  }
    29  
    30  func HostnameValidator(v interface{}) error {
    31  	hostname, valid := v.(string)
    32  	if !valid {
    33  		return errors.New("hostname is not a string")
    34  	}
    35  
    36  	if len(strings.TrimSpace(hostname)) < 1 {
    37  		return errors.New("a value is required")
    38  	}
    39  	if strings.ContainsRune(hostname, '/') || strings.ContainsRune(hostname, ':') {
    40  		return errors.New("invalid hostname")
    41  	}
    42  	return nil
    43  }
    44  
    45  func GraphQLEndpoint(hostname string) string {
    46  	if IsEnterprise(hostname) {
    47  		return fmt.Sprintf("https://%s/api/graphql", hostname)
    48  	}
    49  	return "https://api.github.com/graphql"
    50  }
    51  
    52  func RESTPrefix(hostname string) string {
    53  	if IsEnterprise(hostname) {
    54  		return fmt.Sprintf("https://%s/api/v3/", hostname)
    55  	}
    56  	return "https://api.github.com/"
    57  }
    58  
    59  func GistPrefix(hostname string) string {
    60  	if IsEnterprise(hostname) {
    61  		return fmt.Sprintf("https://%s/gist/", hostname)
    62  	}
    63  	return fmt.Sprintf("https://gist.%s/", hostname)
    64  }