diff --git a/cmd/gh/main.go b/cmd/gh/main.go index 456195314..44357ffd6 100644 --- a/cmd/gh/main.go +++ b/cmd/gh/main.go @@ -92,23 +92,17 @@ func main() { } } - authCheckEnabled := cmdutil.IsAuthCheckEnabled(cmd) - - // TODO support other names - ghtoken := os.Getenv("GITHUB_TOKEN") - if ghtoken != "" { - authCheckEnabled = false - } - + authCheckEnabled := os.Getenv("GITHUB_TOKEN") == "" && + os.Getenv("GITHUB_ENTERPRISE_TOKEN") == "" && + cmdutil.IsAuthCheckEnabled(cmd) if authCheckEnabled { - hasAuth := false - cfg, err := cmdFactory.Config() - if err == nil { - hasAuth = cmdutil.CheckAuth(cfg) + if err != nil { + fmt.Fprintf(stderr, "failed to read configuration: %s\n", err) + os.Exit(2) } - if !hasAuth { + if !cmdutil.CheckAuth(cfg) { fmt.Fprintln(stderr, utils.Bold("Welcome to GitHub CLI!")) fmt.Fprintln(stderr) fmt.Fprintln(stderr, "To authenticate, please run `gh auth login`.") diff --git a/pkg/cmd/factory/http.go b/pkg/cmd/factory/http.go index 1cccfda9c..9daf6d36b 100644 --- a/pkg/cmd/factory/http.go +++ b/pkg/cmd/factory/http.go @@ -23,11 +23,15 @@ func httpClient(io *iostreams.IOStreams, cfg config.Config, appVersion string, s opts = append(opts, api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", appVersion)), api.AddHeaderFunc("Authorization", func(req *http.Request) (string, error) { - if token := os.Getenv("GITHUB_TOKEN"); token != "" { + hostname := ghinstance.NormalizeHostname(req.URL.Hostname()) + + if token := os.Getenv("GITHUB_TOKEN"); hostname == ghinstance.Default() && token != "" { + return fmt.Sprintf("token %s", token), nil + } + if token := os.Getenv("GITHUB_ENTERPRISE_TOKEN"); ghinstance.IsEnterprise(hostname) && token != "" { return fmt.Sprintf("token %s", token), nil } - hostname := ghinstance.NormalizeHostname(req.URL.Hostname()) token, err := cfg.Get(hostname, "oauth_token") if err != nil || token == "" { // Users shouldn't see this because of the pre-execute auth check on commands diff --git a/pkg/cmd/root/root.go b/pkg/cmd/root/root.go index 252636e52..947ba73a8 100644 --- a/pkg/cmd/root/root.go +++ b/pkg/cmd/root/root.go @@ -41,8 +41,10 @@ func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command { Open an issue using “gh issue create -R cli/cli” `), "help:environment": heredoc.Doc(` - GITHUB_TOKEN: an authentication token for API requests. Setting this avoids being - prompted to authenticate and overrides any previously stored credentials. + GITHUB_TOKEN: an authentication token for github.com API requests. Setting this avoids + being prompted to authenticate and takes precedence over previously stored credentials. + + GITHUB_ENTERPRISE_TOKEN: an authentication token for API requests to GitHub Enterprise. GH_REPO: specify the GitHub repository in the "[HOST/]OWNER/REPO" format for commands that otherwise operate on a local repository.