From 3cdd3599873a9c536aeeea1e4e992a20fe74374b Mon Sep 17 00:00:00 2001 From: Benjamin Levesque <14175665+benjlevesque@users.noreply.github.com> Date: Tue, 16 Sep 2025 21:56:18 +0200 Subject: [PATCH] remove showToken from authEntry --- pkg/cmd/auth/status/status.go | 27 ++++++++++++--------------- pkg/cmd/auth/status/status_test.go | 9 ++++----- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/pkg/cmd/auth/status/status.go b/pkg/cmd/auth/status/status.go index 37f97aaa4..2acdb1687 100644 --- a/pkg/cmd/auth/status/status.go +++ b/pkg/cmd/auth/status/status.go @@ -29,8 +29,6 @@ type authEntry struct { Token string `json:"token"` Scopes string `json:"scopes"` GitProtocol string `json:"gitProtocol"` - - showToken bool } var authFields = []string{ @@ -45,7 +43,7 @@ var authFields = []string{ "gitProtocol", } -func (e authEntry) String(cs *iostreams.ColorScheme) string { +func (e authEntry) String(cs *iostreams.ColorScheme, showToken bool) string { var sb strings.Builder switch e.State { case authStateSuccess: @@ -55,7 +53,7 @@ func (e authEntry) String(cs *iostreams.ColorScheme) string { activeStr := fmt.Sprintf("%v", e.Active) sb.WriteString(fmt.Sprintf(" - Active account: %s\n", cs.Bold(activeStr))) sb.WriteString(fmt.Sprintf(" - Git operations protocol: %s\n", cs.Bold(e.GitProtocol))) - sb.WriteString(fmt.Sprintf(" - Token: %s\n", cs.Bold(e.displayToken()))) + sb.WriteString(fmt.Sprintf(" - Token: %s\n", cs.Bold(displayToken(e.Token, showToken)))) if expectScopes(e.Token) { sb.WriteString(fmt.Sprintf(" - Token scopes: %s\n", cs.Bold(displayScopes(e.Scopes)))) @@ -199,6 +197,7 @@ func statusRun(opts *StatusOptions) error { fmt.Fprintf(stderr, "You are not logged into any GitHub hosts. To log in, run: %s\n", cs.Bold("gh auth login")) if opts.Exporter != nil { + // In machine-friendly mode, we always exit with no error. opts.Exporter.Write(opts.IO, struct{}{}) return nil } @@ -209,6 +208,7 @@ func statusRun(opts *StatusOptions) error { fmt.Fprintf(stderr, "You are not logged into any accounts on %s\n", opts.Hostname) if opts.Exporter != nil { + // In machine-friendly mode, we always exit with no error. opts.Exporter.Write(opts.IO, struct{}{}) return nil } @@ -296,7 +296,7 @@ func statusRun(opts *StatusOptions) error { prevEntry = true fmt.Fprintf(stream, "%s\n", cs.Bold(hostname)) for i, entry := range entries { - fmt.Fprintf(stream, "%s", entry.String(cs)) + fmt.Fprintf(stream, "%s", entry.String(cs, showToken)) if i < len(entries)-1 { fmt.Fprint(stream, "\n") } @@ -306,17 +306,17 @@ func statusRun(opts *StatusOptions) error { return err } -func (e authEntry) displayToken() string { - if e.showToken { - return e.Token +func displayToken(token string, printRaw bool) string { + if printRaw { + return token } - if idx := strings.LastIndexByte(e.Token, '_'); idx > -1 { - prefix := e.Token[0 : idx+1] - return prefix + strings.Repeat("*", len(e.Token)-len(prefix)) + if idx := strings.LastIndexByte(token, '_'); idx > -1 { + prefix := token[0 : idx+1] + return prefix + strings.Repeat("*", len(token)-len(prefix)) } - return strings.Repeat("*", len(e.Token)) + return strings.Repeat("*", len(token)) } func displayScopes(scopes string) string { @@ -345,7 +345,6 @@ type buildEntryOptions struct { } func buildEntry(httpClient *http.Client, opts buildEntryOptions) authEntry { - entry := authEntry{ Active: opts.active, Host: opts.hostname, @@ -353,8 +352,6 @@ func buildEntry(httpClient *http.Client, opts buildEntryOptions) authEntry { TokenSource: opts.tokenSource, Token: opts.token, GitProtocol: opts.gitProtocol, - - showToken: opts.showToken, } if opts.tokenSource == "oauth_token" { diff --git a/pkg/cmd/auth/status/status_test.go b/pkg/cmd/auth/status/status_test.go index f5f280cbb..ffb2186af 100644 --- a/pkg/cmd/auth/status/status_test.go +++ b/pkg/cmd/auth/status/status_test.go @@ -22,11 +22,10 @@ import ( func Test_NewCmdStatus(t *testing.T) { tests := []struct { - name string - cli string - wants StatusOptions - wantErr error - wantErrOut string + name string + cli string + wants StatusOptions + wantErr error }{ { name: "no arguments",