diff --git a/internal/config/config_setup.go b/internal/config/config_setup.go index c2ee598c1..649e105b7 100644 --- a/internal/config/config_setup.go +++ b/internal/config/config_setup.go @@ -12,6 +12,7 @@ import ( "github.com/cli/cli/auth" "github.com/cli/cli/pkg/browser" "github.com/cli/cli/utils" + "github.com/mattn/go-colorable" ) var ( @@ -29,7 +30,9 @@ func IsGitHubApp(id string) bool { } func AuthFlowWithConfig(cfg Config, hostname, notice string, additionalScopes []string) (string, error) { - token, userLogin, err := authFlow(hostname, notice, additionalScopes) + stderr := colorable.NewColorableStderr() + + token, userLogin, err := authFlow(hostname, stderr, notice, additionalScopes) if err != nil { return "", err } @@ -48,14 +51,17 @@ func AuthFlowWithConfig(cfg Config, hostname, notice string, additionalScopes [] return "", err } - AuthFlowComplete() + fmt.Fprintf(stderr, "%s Authentication complete. %s to continue...\n", + utils.GreenCheck(), utils.Bold("Press Enter")) + _ = waitForEnter(os.Stdin) + return token, nil } -func authFlow(oauthHost, notice string, additionalScopes []string) (string, string, error) { +func authFlow(oauthHost string, w io.Writer, notice string, additionalScopes []string) (string, string, error) { var verboseStream io.Writer if strings.Contains(os.Getenv("DEBUG"), "oauth") { - verboseStream = os.Stderr + verboseStream = w } minimumScopes := []string{"repo", "read:org", "gist"} @@ -73,9 +79,9 @@ func authFlow(oauthHost, notice string, additionalScopes []string) (string, stri HTTPClient: http.DefaultClient, OpenInBrowser: func(url, code string) error { if code != "" { - fmt.Fprintf(os.Stderr, "%s First copy your one-time code: %s\n", utils.Yellow("!"), utils.Bold(code)) + fmt.Fprintf(w, "%s First copy your one-time code: %s\n", utils.Yellow("!"), utils.Bold(code)) } - fmt.Fprintf(os.Stderr, "- %s to open %s in your browser... ", utils.Bold("Press Enter"), oauthHost) + fmt.Fprintf(w, "- %s to open %s in your browser... ", utils.Bold("Press Enter"), oauthHost) _ = waitForEnter(os.Stdin) browseCmd, err := browser.Command(url) @@ -84,15 +90,15 @@ func authFlow(oauthHost, notice string, additionalScopes []string) (string, stri } err = browseCmd.Run() if err != nil { - fmt.Fprintf(os.Stderr, "%s Failed opening a web browser at %s\n", utils.Red("!"), url) - fmt.Fprintf(os.Stderr, " %s\n", err) - fmt.Fprint(os.Stderr, " Please try entering the URL in your browser manually\n") + fmt.Fprintf(w, "%s Failed opening a web browser at %s\n", utils.Red("!"), url) + fmt.Fprintf(w, " %s\n", err) + fmt.Fprint(w, " Please try entering the URL in your browser manually\n") } return nil }, } - fmt.Fprintln(os.Stderr, notice) + fmt.Fprintln(w, notice) token, err := flow.ObtainAccessToken() if err != nil { @@ -107,12 +113,6 @@ func authFlow(oauthHost, notice string, additionalScopes []string) (string, stri return token, userLogin, nil } -func AuthFlowComplete() { - fmt.Fprintf(os.Stderr, "%s Authentication complete. %s to continue...\n", - utils.GreenCheck(), utils.Bold("Press Enter")) - _ = waitForEnter(os.Stdin) -} - func getViewer(hostname, token string) (string, error) { http := api.NewClient(api.AddHeader("Authorization", fmt.Sprintf("token %s", token))) return api.CurrentLoginName(http, hostname)