Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

Commit

Permalink
Merge pull request #696 from profclems/allow-protocol-override
Browse files Browse the repository at this point in the history
Allow protocol override when overriding the default host
  • Loading branch information
zemzale authored May 3, 2021
2 parents ecdfc7f + 70ea685 commit 0e1ca38
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
4 changes: 3 additions & 1 deletion cmd/glab/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@ func maybeOverrideDefaultHost(f *cmdutils.Factory) {
}
if glHostFromEnv := config.GetFromEnv("host"); glHostFromEnv != "" {
if utils.IsValidURL(glHostFromEnv) {
glHostFromEnv, _ = glinstance.StripHostProtocol(glHostFromEnv)
var protocol string
glHostFromEnv, protocol = glinstance.StripHostProtocol(glHostFromEnv)
glinstance.OverrideDefaultProtocol(protocol)
}
glinstance.OverrideDefault(glHostFromEnv)
}
Expand Down
30 changes: 27 additions & 3 deletions internal/glinstance/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,53 @@ import (
"strings"
)

const defaultHostname = "gitlab.com"
const (
defaultHostname = "gitlab.com"
defaultProtocol = "https"
)

var hostnameOverride string
var protocolOverride string

// Default returns the host name of the default GitLab instance
func Default() string {
return defaultHostname
}

// OverridableDefault is like Default, except it is overridable by the GITLAB_TOKEN environment variable
// DefaultProtocol returns the protocol of the default GitLab instance
func DefaultProtocol() string {
return defaultProtocol
}

// OverridableDefault is like Default, except it is overridable by the GITLAB_HOST environment variable
func OverridableDefault() string {
if hostnameOverride != "" {
return hostnameOverride
}
return Default()
}

// OverridableDefaultProtocol is like DefaultProtocol, except it is overridable by the protocol found in
// the value of the GITLAB_HOST environment variable if a fully qualified URL is given as value
func OverridableDefaultProtocol() string {
if protocolOverride != "" {
return protocolOverride
}
return DefaultProtocol()
}

// 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
}

// OverrideDefaultProtocol overrides the value returned from OverridableDefaultProtocol. This should only ever be
// called from the main runtime path, not tests.
func OverrideDefaultProtocol(newProtocol string) {
protocolOverride = newProtocol
}

// IsSelfHosted reports whether a non-normalized host name looks like a Self-hosted GitLab instance
func IsSelfHosted(h string) bool {
return NormalizeHostname(h) != Default()
Expand Down Expand Up @@ -60,7 +84,7 @@ func StripHostProtocol(h string) (hostname, protocol string) {
// APIEndpoint returns the REST API endpoint prefix for a GitLab instance :)
func APIEndpoint(hostname, protocol string) string {
if protocol == "" {
protocol = "https"
protocol = OverridableDefaultProtocol()
}
if IsSelfHosted(hostname) {
return fmt.Sprintf("%s://%s/api/v4/", protocol, hostname)
Expand Down

0 comments on commit 0e1ca38

Please sign in to comment.