From 7427716ea81fbe4bc6645767d1aca3b9beb05d46 Mon Sep 17 00:00:00 2001 From: nate smith Date: Fri, 11 Oct 2019 10:53:29 -0500 Subject: [PATCH] towards moving config into context --- {github => context}/config.go | 6 +-- {github => context}/config_decoder.go | 2 +- {github => context}/config_encoder.go | 2 +- {github => context}/config_service.go | 2 +- context/context.go | 7 ++- context/ghrepo.go | 14 +++--- github/hosts.go | 64 --------------------------- 7 files changed, 17 insertions(+), 80 deletions(-) rename {github => context}/config.go (98%) rename {github => context}/config_decoder.go (98%) rename {github => context}/config_encoder.go (98%) rename {github => context}/config_service.go (97%) delete mode 100644 github/hosts.go diff --git a/github/config.go b/context/config.go similarity index 98% rename from github/config.go rename to context/config.go index 420f56c19..5ff7fd186 100644 --- a/github/config.go +++ b/context/config.go @@ -1,4 +1,4 @@ -package github +package context import ( "bufio" @@ -41,7 +41,7 @@ func (c *Config) PromptForHost(host string) (h *Host, err error) { token := c.DetectToken() tokenFromEnv := token != "" - if host != GitHubHost { + if host != GitHubHostname() { if _, e := url.Parse("https://" + host); e != nil { err = fmt.Errorf("invalid hostname: %q", host) return @@ -338,7 +338,7 @@ func (c *Config) DefaultHost() (host *Host, err error) { // HACK: forces host to inherit GITHUB_TOKEN if applicable host, err = c.PromptForHost(host.Host) } else { - host, err = c.PromptForHost(DefaultGitHubHost()) + host, err = c.PromptForHost(GitHubHostname()) } return diff --git a/github/config_decoder.go b/context/config_decoder.go similarity index 98% rename from github/config_decoder.go rename to context/config_decoder.go index 7e467b761..8a703c0f8 100644 --- a/github/config_decoder.go +++ b/context/config_decoder.go @@ -1,4 +1,4 @@ -package github +package context import ( "io" diff --git a/github/config_encoder.go b/context/config_encoder.go similarity index 98% rename from github/config_encoder.go rename to context/config_encoder.go index 87609d38d..4b11f9b73 100644 --- a/github/config_encoder.go +++ b/context/config_encoder.go @@ -1,4 +1,4 @@ -package github +package context import ( "io" diff --git a/github/config_service.go b/context/config_service.go similarity index 97% rename from github/config_service.go rename to context/config_service.go index 3aaa6915f..121517e2c 100644 --- a/github/config_service.go +++ b/context/config_service.go @@ -1,4 +1,4 @@ -package github +package context import ( "os" diff --git a/context/context.go b/context/context.go index 85af0d8f9..68695ddcf 100644 --- a/context/context.go +++ b/context/context.go @@ -7,7 +7,6 @@ import ( "strings" "github.com/github/gh-cli/git" - "github.com/github/gh-cli/github" ) // GetToken returns the authentication token as stored in the config file for the user running gh-cli @@ -28,7 +27,7 @@ func GetToken() (string, error) { } func CurrentUsername() (string, error) { - host, err := github.CurrentConfig().DefaultHost() + host, err := CurrentConfig().DefaultHost() if err != nil { return "", nil } @@ -43,3 +42,7 @@ func CurrentBranch() (string, error) { return strings.Replace(currentBranch, "refs/heads/", "", 1), nil } + +func GitHubHostname() string { + return "github.com" +} diff --git a/context/ghrepo.go b/context/ghrepo.go index 5ea9a45a6..bfb8b8f84 100644 --- a/context/ghrepo.go +++ b/context/ghrepo.go @@ -5,8 +5,6 @@ import ( "net/url" "os" "strings" - - "github.com/github/gh-cli/github" ) type GitHubRepository struct { @@ -21,7 +19,7 @@ func CurrentGitHubRepository() (*GitHubRepository, error) { var repoURL *url.URL var err error if repoFromEnv := os.Getenv("GH_REPO"); repoFromEnv != "" { - repoURL, err = url.Parse(fmt.Sprintf("https://github.com/%s.git", repoFromEnv)) + repoURL, err = url.Parse(fmt.Sprintf("https://%s/%s.git", GitHubHostname(), repoFromEnv)) if err != nil { return nil, err } @@ -35,7 +33,7 @@ func CurrentGitHubRepository() (*GitHubRepository, error) { } urlError := fmt.Errorf("invalid GitHub URL: %s", repoURL) - if !github.KnownGitHubHostsInclude(repoURL.Host) { + if repoURL.Host != GitHubHostname() && repoURL.Host != fmt.Sprintf("ssh.%s", GitHubHostname()) { return nil, urlError } @@ -64,17 +62,17 @@ func CurrentGitHubRepository() (*GitHubRepository, error) { } if host == "" { - host = github.DefaultGitHubHost() + host = GitHubHostname() } if host == "ssh.github.com" { - host = github.GitHubHost + host = GitHubHostname() } if protocol != "http" && protocol != "https" { protocol = "" } if protocol == "" { - h := github.CurrentConfig().Find(host) + h := CurrentConfig().Find(host) if h != nil { protocol = h.Protocol } @@ -84,7 +82,7 @@ func CurrentGitHubRepository() (*GitHubRepository, error) { } if owner == "" { - h := github.CurrentConfig().Find(host) + h := CurrentConfig().Find(host) if h != nil { owner = h.User } diff --git a/github/hosts.go b/github/hosts.go deleted file mode 100644 index a74d9810c..000000000 --- a/github/hosts.go +++ /dev/null @@ -1,64 +0,0 @@ -package github - -import ( - "fmt" - "net/url" - "os" - "strings" - - "github.com/github/gh-cli/git" -) - -var ( - GitHubHostEnv = os.Getenv("GITHUB_HOST") - cachedHosts []string -) - -type GithubHostError struct { - url *url.URL -} - -func (e *GithubHostError) Error() string { - return fmt.Sprintf("Invalid GitHub URL: %s", e.url) -} - -func KnownGitHubHostsInclude(host string) bool { - for _, hh := range knownGitHubHosts() { - if hh == host { - return true - } - } - - return false -} - -func knownGitHubHosts() []string { - if cachedHosts != nil { - return cachedHosts - } - - hosts := []string{} - defaultHost := DefaultGitHubHost() - hosts = append(hosts, defaultHost) - hosts = append(hosts, "ssh.github.com") - - ghHosts, _ := git.ConfigAll("hub.host") - for _, ghHost := range ghHosts { - ghHost = strings.TrimSpace(ghHost) - if ghHost != "" { - hosts = append(hosts, ghHost) - } - } - - cachedHosts = hosts - return hosts -} - -func DefaultGitHubHost() string { - defaultHost := GitHubHostEnv - if defaultHost == "" { - defaultHost = GitHubHost - } - - return defaultHost -}