diff --git a/cli.go b/cli.go index ba4e802da5..428d77680b 100644 --- a/cli.go +++ b/cli.go @@ -2,7 +2,6 @@ package main import ( "errors" - "net/url" "os" "path/filepath" "runtime" @@ -196,7 +195,7 @@ func apiToken() string { return key } netrc := getNetrc() - machine := netrc.FindMachine(herokuAPIHost()) + machine := netrc.FindMachine(apiHost()) if machine != nil { return machine.Password } @@ -238,24 +237,3 @@ func (cli *Cli) AddCommand(command *Command) bool { cli.Commands = append(cli.Commands, command) return true } - -func herokuAPIHost() string { - env := os.Getenv("HEROKU_API_URL") - if env == "" { - return "api.heroku.com" - } - apiURL, err := url.Parse(env) - ExitIfError(err) - return apiURL.Host -} - -func herokuGitHost() string { - if herokuAPIHost() == "api.heroku.com" { - return "git.heroku.com" - } - return "git." + herokuAPIHost() -} - -func shouldVerifyHost() bool { - return !strings.HasSuffix(herokuAPIHost(), "herokudev.com") -} diff --git a/git.go b/git.go index 685f1f295e..6a5b238f6c 100644 --- a/git.go +++ b/git.go @@ -11,14 +11,29 @@ import ( "syscall" ) +func host() string { + if host := os.Getenv("HEROKU_HOST"); host != "" { + return host + } + return "heroku.com" +} + +func apiHost() string { + return "api." + host() +} + func gitHost() string { - if herokuGitHost := os.Getenv("HEROKU_GIT_HOST"); herokuGitHost != "" { - return herokuGitHost + if host := os.Getenv("HEROKU_GIT_HOST"); host != "" { + return host } - if herokuHost := os.Getenv("HEROKU_HOST"); herokuHost != "" { - return herokuHost + return host() +} + +func httpGitHost() string { + if host := os.Getenv("HEROKU_HTTP_GIT_HOST"); host != "" { + return host } - return "heroku.com" + return "git." + host() } func gitURLPre() string { @@ -26,7 +41,7 @@ func gitURLPre() string { } func gitHTTPSURLPre() string { - return "https://git.heroku.com/" + return "https://" + httpGitHost() + "/" } func gitRemotes() (map[string]string, error) { diff --git a/login.go b/login.go index 722dbb461e..11c851a37c 100644 --- a/login.go +++ b/login.go @@ -47,9 +47,11 @@ func login() { func saveOauthToken(email, token string) { netrc := getNetrc() + netrc.RemoveMachine(apiHost()) + netrc.RemoveMachine(httpGitHost()) netrc.Filename = netrcPath() - netrc.NewMachine(herokuAPIHost(), email, token, "") - netrc.NewMachine(herokuGitHost(), email, token, "") + netrc.NewMachine(apiHost(), email, token, "") + netrc.NewMachine(httpGitHost(), email, token, "") ExitIfError(netrc.Save()) } diff --git a/request.go b/request.go index 1a677210d7..87a70cc498 100644 --- a/request.go +++ b/request.go @@ -1,16 +1,24 @@ package main -import "github.com/franela/goreq" +import ( + "strings" + + "github.com/franela/goreq" +) func apiRequest(authToken string) *goreq.Request { req := goreq.Request{ - Uri: "https://" + herokuAPIHost(), + Uri: "https://" + apiHost(), Accept: "application/vnd.heroku+json; version=3", ShowDebug: debugging, - Insecure: !shouldVerifyHost(), + Insecure: !shouldVerifyHost(apiHost()), } if authToken != "" { req.AddHeader("Authorization", "Bearer "+authToken) } return &req } + +func shouldVerifyHost(host string) bool { + return !strings.HasSuffix(host, "herokudev.com") +}