towards moving config into context
This commit is contained in:
parent
3b635d5260
commit
7427716ea8
7 changed files with 17 additions and 80 deletions
|
|
@ -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
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package github
|
||||
package context
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package github
|
||||
package context
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package github
|
||||
package context
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
|
@ -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"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue