X Tutup
Skip to content

Commit a8b06c3

Browse files
committed
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.
1 parent cb4cc72 commit a8b06c3

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

internal/config/config_setup.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/cli/cli/auth"
1313
"github.com/cli/cli/pkg/browser"
1414
"github.com/cli/cli/utils"
15+
"github.com/mattn/go-colorable"
1516
)
1617

1718
var (
@@ -29,7 +30,9 @@ func IsGitHubApp(id string) bool {
2930
}
3031

3132
func AuthFlowWithConfig(cfg Config, hostname, notice string, additionalScopes []string) (string, error) {
32-
token, userLogin, err := authFlow(hostname, notice, additionalScopes)
33+
stderr := colorable.NewColorableStderr()
34+
35+
token, userLogin, err := authFlow(hostname, stderr, notice, additionalScopes)
3336
if err != nil {
3437
return "", err
3538
}
@@ -48,14 +51,17 @@ func AuthFlowWithConfig(cfg Config, hostname, notice string, additionalScopes []
4851
return "", err
4952
}
5053

51-
AuthFlowComplete()
54+
fmt.Fprintf(stderr, "%s Authentication complete. %s to continue...\n",
55+
utils.GreenCheck(), utils.Bold("Press Enter"))
56+
_ = waitForEnter(os.Stdin)
57+
5258
return token, nil
5359
}
5460

55-
func authFlow(oauthHost, notice string, additionalScopes []string) (string, string, error) {
61+
func authFlow(oauthHost string, w io.Writer, notice string, additionalScopes []string) (string, string, error) {
5662
var verboseStream io.Writer
5763
if strings.Contains(os.Getenv("DEBUG"), "oauth") {
58-
verboseStream = os.Stderr
64+
verboseStream = w
5965
}
6066

6167
minimumScopes := []string{"repo", "read:org", "gist"}
@@ -73,9 +79,9 @@ func authFlow(oauthHost, notice string, additionalScopes []string) (string, stri
7379
HTTPClient: http.DefaultClient,
7480
OpenInBrowser: func(url, code string) error {
7581
if code != "" {
76-
fmt.Fprintf(os.Stderr, "%s First copy your one-time code: %s\n", utils.Yellow("!"), utils.Bold(code))
82+
fmt.Fprintf(w, "%s First copy your one-time code: %s\n", utils.Yellow("!"), utils.Bold(code))
7783
}
78-
fmt.Fprintf(os.Stderr, "- %s to open %s in your browser... ", utils.Bold("Press Enter"), oauthHost)
84+
fmt.Fprintf(w, "- %s to open %s in your browser... ", utils.Bold("Press Enter"), oauthHost)
7985
_ = waitForEnter(os.Stdin)
8086

8187
browseCmd, err := browser.Command(url)
@@ -84,15 +90,15 @@ func authFlow(oauthHost, notice string, additionalScopes []string) (string, stri
8490
}
8591
err = browseCmd.Run()
8692
if err != nil {
87-
fmt.Fprintf(os.Stderr, "%s Failed opening a web browser at %s\n", utils.Red("!"), url)
88-
fmt.Fprintf(os.Stderr, " %s\n", err)
89-
fmt.Fprint(os.Stderr, " Please try entering the URL in your browser manually\n")
93+
fmt.Fprintf(w, "%s Failed opening a web browser at %s\n", utils.Red("!"), url)
94+
fmt.Fprintf(w, " %s\n", err)
95+
fmt.Fprint(w, " Please try entering the URL in your browser manually\n")
9096
}
9197
return nil
9298
},
9399
}
94100

95-
fmt.Fprintln(os.Stderr, notice)
101+
fmt.Fprintln(w, notice)
96102

97103
token, err := flow.ObtainAccessToken()
98104
if err != nil {
@@ -107,12 +113,6 @@ func authFlow(oauthHost, notice string, additionalScopes []string) (string, stri
107113
return token, userLogin, nil
108114
}
109115

110-
func AuthFlowComplete() {
111-
fmt.Fprintf(os.Stderr, "%s Authentication complete. %s to continue...\n",
112-
utils.GreenCheck(), utils.Bold("Press Enter"))
113-
_ = waitForEnter(os.Stdin)
114-
}
115-
116116
func getViewer(hostname, token string) (string, error) {
117117
http := api.NewClient(api.AddHeader("Authorization", fmt.Sprintf("token %s", token)))
118118
return api.CurrentLoginName(http, hostname)

0 commit comments

Comments
 (0)
X Tutup