Ensure correct ANSI color output during OAuth flow on Windows

We used to write directly to `os.Stderr`, but we first need to convert
that into a colorable stream.
This commit is contained in:
Mislav Marohnić 2020-09-01 12:31:36 +02:00
parent cb4cc72e50
commit a8b06c329b

View file

@ -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)