X Tutup
Skip to content

Commit 24d863e

Browse files
author
Nate Smith
authored
Merge pull request cli#2316 from cli/colors-1624
migrate off of utils color functions
2 parents d79eb49 + 47eef41 commit 24d863e

File tree

36 files changed

+298
-308
lines changed

36 files changed

+298
-308
lines changed

cmd/gh/main.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/cli/cli/pkg/cmdutil"
2323
"github.com/cli/cli/update"
2424
"github.com/cli/cli/utils"
25+
"github.com/mattn/go-colorable"
2526
"github.com/mgutz/ansi"
2627
"github.com/spf13/cobra"
2728
)
@@ -124,12 +125,14 @@ func main() {
124125
}
125126
}
126127

128+
cs := cmdFactory.IOStreams.ColorScheme()
129+
127130
authCheckEnabled := os.Getenv("GITHUB_TOKEN") == "" &&
128131
os.Getenv("GITHUB_ENTERPRISE_TOKEN") == "" &&
129132
cmd != nil && cmdutil.IsAuthCheckEnabled(cmd)
130133
if authCheckEnabled {
131134
if !cmdutil.CheckAuth(cfg) {
132-
fmt.Fprintln(stderr, utils.Bold("Welcome to GitHub CLI!"))
135+
fmt.Fprintln(stderr, cs.Bold("Welcome to GitHub CLI!"))
133136
fmt.Fprintln(stderr)
134137
fmt.Fprintln(stderr, "To authenticate, please run `gh auth login`.")
135138
fmt.Fprintln(stderr, "You can also set the GITHUB_TOKEN environment variable, if preferred.")
@@ -251,5 +254,5 @@ func basicClient(currentVersion string) (*api.Client, error) {
251254
func apiVerboseLog() api.ClientOption {
252255
logTraffic := strings.Contains(os.Getenv("DEBUG"), "api")
253256
colorize := utils.IsTerminal(os.Stderr)
254-
return api.VerboseLog(utils.NewColorable(os.Stderr), logTraffic, colorize)
257+
return api.VerboseLog(colorable.NewColorable(os.Stderr), logTraffic, colorize)
255258
}

internal/authflow/flow.go

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

1918
var (
@@ -23,12 +22,13 @@ var (
2322
oauthClientSecret = "34ddeff2b558a23d38fba8a6de74f086ede1cc0b"
2423
)
2524

26-
func AuthFlowWithConfig(cfg config.Config, hostname, notice string, additionalScopes []string) (string, error) {
25+
func AuthFlowWithConfig(cfg config.Config, IO *iostreams.IOStreams, hostname, notice string, additionalScopes []string) (string, error) {
2726
// TODO this probably shouldn't live in this package. It should probably be in a new package that
2827
// depends on both iostreams and config.
29-
stderr := colorable.NewColorableStderr()
28+
stderr := IO.ErrOut
29+
cs := IO.ColorScheme()
3030

31-
token, userLogin, err := authFlow(hostname, stderr, notice, additionalScopes)
31+
token, userLogin, err := authFlow(hostname, IO, notice, additionalScopes)
3232
if err != nil {
3333
return "", err
3434
}
@@ -48,13 +48,16 @@ func AuthFlowWithConfig(cfg config.Config, hostname, notice string, additionalSc
4848
}
4949

5050
fmt.Fprintf(stderr, "%s Authentication complete. %s to continue...\n",
51-
utils.GreenCheck(), utils.Bold("Press Enter"))
52-
_ = waitForEnter(os.Stdin)
51+
cs.SuccessIcon(), cs.Bold("Press Enter"))
52+
_ = waitForEnter(IO.In)
5353

5454
return token, nil
5555
}
5656

57-
func authFlow(oauthHost string, w io.Writer, notice string, additionalScopes []string) (string, string, error) {
57+
func authFlow(oauthHost string, IO *iostreams.IOStreams, notice string, additionalScopes []string) (string, string, error) {
58+
w := IO.ErrOut
59+
cs := IO.ColorScheme()
60+
5861
var verboseStream io.Writer
5962
if strings.Contains(os.Getenv("DEBUG"), "oauth") {
6063
verboseStream = w
@@ -75,18 +78,18 @@ func authFlow(oauthHost string, w io.Writer, notice string, additionalScopes []s
7578
HTTPClient: http.DefaultClient,
7679
OpenInBrowser: func(url, code string) error {
7780
if code != "" {
78-
fmt.Fprintf(w, "%s First copy your one-time code: %s\n", utils.Yellow("!"), utils.Bold(code))
81+
fmt.Fprintf(w, "%s First copy your one-time code: %s\n", cs.Yellow("!"), cs.Bold(code))
7982
}
80-
fmt.Fprintf(w, "- %s to open %s in your browser... ", utils.Bold("Press Enter"), oauthHost)
81-
_ = waitForEnter(os.Stdin)
83+
fmt.Fprintf(w, "- %s to open %s in your browser... ", cs.Bold("Press Enter"), oauthHost)
84+
_ = waitForEnter(IO.In)
8285

8386
browseCmd, err := browser.Command(url)
8487
if err != nil {
8588
return err
8689
}
8790
err = browseCmd.Run()
8891
if err != nil {
89-
fmt.Fprintf(w, "%s Failed opening a web browser at %s\n", utils.Red("!"), url)
92+
fmt.Fprintf(w, "%s Failed opening a web browser at %s\n", cs.Red("!"), url)
9093
fmt.Fprintf(w, " %s\n", err)
9194
fmt.Fprint(w, " Please try entering the URL in your browser manually\n")
9295
}

pkg/cmd/alias/delete/delete.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/cli/cli/internal/config"
77
"github.com/cli/cli/pkg/cmdutil"
88
"github.com/cli/cli/pkg/iostreams"
9-
"github.com/cli/cli/utils"
109
"github.com/spf13/cobra"
1110
)
1211

@@ -63,7 +62,7 @@ func deleteRun(opts *DeleteOptions) error {
6362
}
6463

6564
if opts.IO.IsStdoutTTY() {
66-
redCheck := utils.Red("✓")
65+
redCheck := opts.IO.ColorScheme().Red("✓")
6766
fmt.Fprintf(opts.IO.ErrOut, "%s Deleted alias %s; was %s\n", redCheck, opts.Name, expansion)
6867
}
6968

pkg/cmd/alias/set/set.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/cli/cli/internal/config"
99
"github.com/cli/cli/pkg/cmdutil"
1010
"github.com/cli/cli/pkg/iostreams"
11-
"github.com/cli/cli/utils"
1211
"github.com/google/shlex"
1312
"github.com/spf13/cobra"
1413
)
@@ -84,6 +83,7 @@ func NewCmdSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command
8483
}
8584

8685
func setRun(opts *SetOptions) error {
86+
cs := opts.IO.ColorScheme()
8787
cfg, err := opts.Config()
8888
if err != nil {
8989
return err
@@ -96,7 +96,7 @@ func setRun(opts *SetOptions) error {
9696

9797
isTerminal := opts.IO.IsStdoutTTY()
9898
if isTerminal {
99-
fmt.Fprintf(opts.IO.ErrOut, "- Adding alias for %s: %s\n", utils.Bold(opts.Name), utils.Bold(opts.Expansion))
99+
fmt.Fprintf(opts.IO.ErrOut, "- Adding alias for %s: %s\n", cs.Bold(opts.Name), cs.Bold(opts.Expansion))
100100
}
101101

102102
expansion := opts.Expansion
@@ -114,13 +114,13 @@ func setRun(opts *SetOptions) error {
114114
return fmt.Errorf("could not create alias: %s does not correspond to a gh command", expansion)
115115
}
116116

117-
successMsg := fmt.Sprintf("%s Added alias.", utils.Green("✓"))
117+
successMsg := fmt.Sprintf("%s Added alias.", cs.SuccessIcon())
118118
if oldExpansion, ok := aliasCfg.Get(opts.Name); ok {
119119
successMsg = fmt.Sprintf("%s Changed alias %s from %s to %s",
120-
utils.Green("✓"),
121-
utils.Bold(opts.Name),
122-
utils.Bold(oldExpansion),
123-
utils.Bold(expansion),
120+
cs.SuccessIcon(),
121+
cs.Bold(opts.Name),
122+
cs.Bold(oldExpansion),
123+
cs.Bold(expansion),
124124
)
125125
}
126126

pkg/cmd/auth/login/login.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/cli/cli/pkg/cmdutil"
1717
"github.com/cli/cli/pkg/iostreams"
1818
"github.com/cli/cli/pkg/prompt"
19-
"github.com/cli/cli/utils"
2019
"github.com/spf13/cobra"
2120
)
2221

@@ -228,7 +227,7 @@ func loginRun(opts *LoginOptions) error {
228227
}
229228

230229
if authMode == 0 {
231-
_, err := authflow.AuthFlowWithConfig(cfg, hostname, "", opts.Scopes)
230+
_, err := authflow.AuthFlowWithConfig(cfg, opts.IO, hostname, "", opts.Scopes)
232231
if err != nil {
233232
return fmt.Errorf("failed to authenticate via web browser: %w", err)
234233
}
@@ -258,6 +257,8 @@ func loginRun(opts *LoginOptions) error {
258257
}
259258
}
260259

260+
cs := opts.IO.ColorScheme()
261+
261262
gitProtocol := "https"
262263
if opts.Interactive {
263264
err = prompt.SurveyAskOne(&survey.Select{
@@ -279,7 +280,7 @@ func loginRun(opts *LoginOptions) error {
279280
return err
280281
}
281282

282-
fmt.Fprintf(opts.IO.ErrOut, "%s Configured git protocol\n", utils.GreenCheck())
283+
fmt.Fprintf(opts.IO.ErrOut, "%s Configured git protocol\n", cs.SuccessIcon())
283284
}
284285

285286
apiClient, err := client.ClientFromCfg(hostname, cfg)
@@ -302,7 +303,7 @@ func loginRun(opts *LoginOptions) error {
302303
return err
303304
}
304305

305-
fmt.Fprintf(opts.IO.ErrOut, "%s Logged in as %s\n", utils.GreenCheck(), utils.Bold(username))
306+
fmt.Fprintf(opts.IO.ErrOut, "%s Logged in as %s\n", cs.SuccessIcon(), cs.Bold(username))
306307

307308
return nil
308309
}

pkg/cmd/auth/logout/logout.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/cli/cli/pkg/cmdutil"
1313
"github.com/cli/cli/pkg/iostreams"
1414
"github.com/cli/cli/pkg/prompt"
15-
"github.com/cli/cli/utils"
1615
"github.com/spf13/cobra"
1716
)
1817

@@ -151,8 +150,9 @@ func logoutRun(opts *LogoutOptions) error {
151150
isTTY := opts.IO.IsStdinTTY() && opts.IO.IsStdoutTTY()
152151

153152
if isTTY {
153+
cs := opts.IO.ColorScheme()
154154
fmt.Fprintf(opts.IO.ErrOut, "%s Logged out of %s%s\n",
155-
utils.GreenCheck(), utils.Bold(hostname), usernameStr)
155+
cs.SuccessIcon(), cs.Bold(hostname), usernameStr)
156156
}
157157

158158
return nil

pkg/cmd/auth/refresh/refresh.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ type RefreshOptions struct {
2020

2121
Hostname string
2222
Scopes []string
23-
AuthFlow func(config.Config, string, []string) error
23+
AuthFlow func(config.Config, *iostreams.IOStreams, string, []string) error
2424
}
2525

2626
func NewCmdRefresh(f *cmdutil.Factory, runF func(*RefreshOptions) error) *cobra.Command {
2727
opts := &RefreshOptions{
2828
IO: f.IOStreams,
2929
Config: f.Config,
30-
AuthFlow: func(cfg config.Config, hostname string, scopes []string) error {
31-
_, err := authflow.AuthFlowWithConfig(cfg, hostname, "", scopes)
30+
AuthFlow: func(cfg config.Config, io *iostreams.IOStreams, hostname string, scopes []string) error {
31+
_, err := authflow.AuthFlowWithConfig(cfg, io, hostname, "", scopes)
3232
return err
3333
},
3434
}
@@ -118,5 +118,5 @@ func refreshRun(opts *RefreshOptions) error {
118118
return err
119119
}
120120

121-
return opts.AuthFlow(cfg, hostname, opts.Scopes)
121+
return opts.AuthFlow(cfg, opts.IO, hostname, opts.Scopes)
122122
}

pkg/cmd/auth/refresh/refresh_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func Test_refreshRun(t *testing.T) {
213213
for _, tt := range tests {
214214
t.Run(tt.name, func(t *testing.T) {
215215
aa := authArgs{}
216-
tt.opts.AuthFlow = func(_ config.Config, hostname string, scopes []string) error {
216+
tt.opts.AuthFlow = func(_ config.Config, _ *iostreams.IOStreams, hostname string, scopes []string) error {
217217
aa.hostname = hostname
218218
aa.scopes = scopes
219219
return nil

pkg/cmd/auth/status/status.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/cli/cli/internal/config"
1111
"github.com/cli/cli/pkg/cmdutil"
1212
"github.com/cli/cli/pkg/iostreams"
13-
"github.com/cli/cli/utils"
1413
"github.com/spf13/cobra"
1514
)
1615

@@ -64,12 +63,14 @@ func statusRun(opts *StatusOptions) error {
6463

6564
stderr := opts.IO.ErrOut
6665

66+
cs := opts.IO.ColorScheme()
67+
6768
statusInfo := map[string][]string{}
6869

6970
hostnames, err := cfg.Hosts()
7071
if len(hostnames) == 0 || err != nil {
7172
fmt.Fprintf(stderr,
72-
"You are not logged into any GitHub hosts. Run %s to authenticate.\n", utils.Bold("gh auth login"))
73+
"You are not logged into any GitHub hosts. Run %s to authenticate.\n", cs.Bold("gh auth login"))
7374
return cmdutil.SilentError
7475
}
7576

@@ -98,39 +99,39 @@ func statusRun(opts *StatusOptions) error {
9899
if err != nil {
99100
var missingScopes *api.MissingScopesError
100101
if errors.As(err, &missingScopes) {
101-
addMsg("%s %s: the token in %s is %s", utils.Red("X"), hostname, tokenSource, err)
102+
addMsg("%s %s: the token in %s is %s", cs.Red("X"), hostname, tokenSource, err)
102103
if tokenIsWriteable {
103104
addMsg("- To request missing scopes, run: %s %s\n",
104-
utils.Bold("gh auth refresh -h"),
105-
utils.Bold(hostname))
105+
cs.Bold("gh auth refresh -h"),
106+
cs.Bold(hostname))
106107
}
107108
} else {
108-
addMsg("%s %s: authentication failed", utils.Red("X"), hostname)
109-
addMsg("- The %s token in %s is no longer valid.", utils.Bold(hostname), tokenSource)
109+
addMsg("%s %s: authentication failed", cs.Red("X"), hostname)
110+
addMsg("- The %s token in %s is no longer valid.", cs.Bold(hostname), tokenSource)
110111
if tokenIsWriteable {
111112
addMsg("- To re-authenticate, run: %s %s",
112-
utils.Bold("gh auth login -h"), utils.Bold(hostname))
113+
cs.Bold("gh auth login -h"), cs.Bold(hostname))
113114
addMsg("- To forget about this host, run: %s %s",
114-
utils.Bold("gh auth logout -h"), utils.Bold(hostname))
115+
cs.Bold("gh auth logout -h"), cs.Bold(hostname))
115116
}
116117
}
117118
failed = true
118119
} else {
119120
username, err := api.CurrentLoginName(apiClient, hostname)
120121
if err != nil {
121-
addMsg("%s %s: api call failed: %s", utils.Red("X"), hostname, err)
122+
addMsg("%s %s: api call failed: %s", cs.Red("X"), hostname, err)
122123
}
123-
addMsg("%s Logged in to %s as %s (%s)", utils.GreenCheck(), hostname, utils.Bold(username), tokenSource)
124+
addMsg("%s Logged in to %s as %s (%s)", cs.SuccessIcon(), hostname, cs.Bold(username), tokenSource)
124125
proto, _ := cfg.Get(hostname, "git_protocol")
125126
if proto != "" {
126127
addMsg("%s Git operations for %s configured to use %s protocol.",
127-
utils.GreenCheck(), hostname, utils.Bold(proto))
128+
cs.SuccessIcon(), hostname, cs.Bold(proto))
128129
}
129130
tokenDisplay := "*******************"
130131
if opts.ShowToken {
131132
tokenDisplay = token
132133
}
133-
addMsg("%s Token: %s", utils.GreenCheck(), tokenDisplay)
134+
addMsg("%s Token: %s", cs.SuccessIcon(), tokenDisplay)
134135
}
135136
addMsg("")
136137

@@ -143,7 +144,7 @@ func statusRun(opts *StatusOptions) error {
143144
if !ok {
144145
continue
145146
}
146-
fmt.Fprintf(stderr, "%s\n", utils.Bold(hostname))
147+
fmt.Fprintf(stderr, "%s\n", cs.Bold(hostname))
147148
for _, line := range lines {
148149
fmt.Fprintf(stderr, " %s\n", line)
149150
}

pkg/cmd/gist/create/create.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"github.com/cli/cli/pkg/cmd/gist/shared"
2020
"github.com/cli/cli/pkg/cmdutil"
2121
"github.com/cli/cli/pkg/iostreams"
22-
"github.com/cli/cli/utils"
2322
"github.com/spf13/cobra"
2423
)
2524

@@ -116,8 +115,10 @@ func createRun(opts *CreateOptions) error {
116115
completionMessage = fmt.Sprintf("Created gist %s", gistName)
117116
}
118117

118+
cs := opts.IO.ColorScheme()
119+
119120
errOut := opts.IO.ErrOut
120-
fmt.Fprintf(errOut, "%s %s\n", utils.Gray("-"), processMessage)
121+
fmt.Fprintf(errOut, "%s %s\n", cs.Gray("-"), processMessage)
121122

122123
httpClient, err := opts.HttpClient()
123124
if err != nil {
@@ -132,10 +133,10 @@ func createRun(opts *CreateOptions) error {
132133
return fmt.Errorf("This command requires the 'gist' OAuth scope.\nPlease re-authenticate by doing `gh config set -h github.com oauth_token ''` and running the command again.")
133134
}
134135
}
135-
return fmt.Errorf("%s Failed to create gist: %w", utils.Red("X"), err)
136+
return fmt.Errorf("%s Failed to create gist: %w", cs.Red("X"), err)
136137
}
137138

138-
fmt.Fprintf(errOut, "%s %s\n", utils.Green("✓"), completionMessage)
139+
fmt.Fprintf(errOut, "%s %s\n", cs.SuccessIcon(), completionMessage)
139140

140141
fmt.Fprintln(opts.IO.Out, gist.HTMLURL)
141142

0 commit comments

Comments
 (0)
X Tutup