Skip to content

Commit 3a33735

Browse files
committed
be more helpful
1 parent f358e60 commit 3a33735

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

pkg/cmd/auth/status/status.go

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,25 +88,41 @@ func statusRun(opts *StatusOptions) error {
8888
if err != nil {
8989
var missingScopes *api.MissingScopesError
9090
if errors.As(err, &missingScopes) {
91-
return fmt.Errorf("%s %s: %s", utils.Red("X"), hostname, err)
91+
fmt.Fprintf(stderr, "%s %s: %s\n", utils.Red("X"), hostname, err)
92+
fmt.Fprintln(stderr,
93+
"The token in GITHUB_TOKEN is valid but missing scopes that gh requires to function.")
9294
} else {
93-
return fmt.Errorf("%s %s: authentication failed", utils.Red("X"), hostname)
95+
fmt.Fprintf(stderr, "%s %s: authentication failed\n", utils.Red("X"), hostname)
96+
fmt.Fprintln(stderr)
97+
fmt.Fprintf(stderr,
98+
"The token in GITHUB_TOKEN is invalid.\n")
9499
}
100+
fmt.Fprintf(stderr,
101+
"Please visit https://%s/settings/tokens and create a new token with 'repo', 'read:org', and 'gist' scopes.\n", hostname)
102+
return cmdutil.SilentError
95103
} else {
96104
username, err := api.CurrentLoginName(apiClient, hostname)
97105
if err != nil {
98106
return fmt.Errorf("%s %s: api call failed: %s\n", utils.Red("X"), hostname, err)
99107
}
100-
fmt.Fprintf(stderr, "%s token valid for %s as %s\n", utils.GreenCheck(), hostname, utils.Bold(username))
108+
fmt.Fprintf(stderr,
109+
"%s token valid for %s as %s\n", utils.GreenCheck(), hostname, utils.Bold(username))
110+
proto, _ := cfg.Get(hostname, "git_protocol")
111+
if proto != "" {
112+
fmt.Fprintln(stderr)
113+
fmt.Fprintf(stderr,
114+
"Git operations for %s configured to use %s protocol.\n", hostname, utils.Bold(proto))
115+
}
101116
}
102117

103118
return nil
104119
}
105120

106121
hostnames, err := cfg.Hosts()
107122
if len(hostnames) == 0 || err != nil {
108-
fmt.Fprintf(stderr, "You are not logged into any GitHub hosts. Run 'gh auth login' to authenticate.\n")
109-
return nil
123+
fmt.Fprintf(stderr,
124+
"You are not logged into any GitHub hosts. Run %s to authenticate.\n", utils.Bold("gh auth login"))
125+
return cmdutil.SilentError
110126
}
111127

112128
httpClient, err := opts.HttpClient()
@@ -126,8 +142,17 @@ func statusRun(opts *StatusOptions) error {
126142
var missingScopes *api.MissingScopesError
127143
if errors.As(err, &missingScopes) {
128144
fmt.Fprintf(stderr, "%s %s: %s\n", utils.Red("X"), hostname, err)
145+
fmt.Fprintf(stderr, "To enable the missing scopes, please run %s %s\n",
146+
utils.Bold("gh auth refresh -h"),
147+
utils.Bold(hostname))
129148
} else {
130149
fmt.Fprintf(stderr, "%s %s: authentication failed\n", utils.Red("X"), hostname)
150+
fmt.Fprintln(stderr)
151+
fmt.Fprintf(stderr, "The configured token for %s is no longer valid.\n", utils.Bold(hostname))
152+
fmt.Fprintf(stderr, "To re-authenticate, please run %s %s\n",
153+
utils.Bold("gh auth login -h"), utils.Bold(hostname))
154+
fmt.Fprintf(stderr, "To forget about this host, please run %s %s\n",
155+
utils.Bold("gh auth logout -h"), utils.Bold(hostname))
131156
}
132157
failed = true
133158
} else {
@@ -136,6 +161,13 @@ func statusRun(opts *StatusOptions) error {
136161
fmt.Fprintf(stderr, "%s %s: api call failed: %s\n", utils.Red("X"), hostname, err)
137162
}
138163
fmt.Fprintf(stderr, "%s Logged in to %s as %s\n", utils.GreenCheck(), hostname, utils.Bold(username))
164+
proto, _ := cfg.Get(hostname, "git_protocol")
165+
if proto != "" {
166+
fmt.Fprintln(stderr)
167+
fmt.Fprintf(stderr,
168+
"Git operations for %s configured to use %s protocol.\n", hostname, utils.Bold(proto))
169+
fmt.Fprintln(stderr)
170+
}
139171
}
140172

141173
// NB we could take this opportunity to add or fix the "user" key in the hosts config. I chose

pkg/cmd/auth/status/status_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ func Test_statusRun(t *testing.T) {
113113
httpmock.StatusStringResponse(400, "no bueno"),
114114
)
115115
},
116-
wantErr: regexp.MustCompile(`authentication failed`),
116+
wantErr: regexp.MustCompile(``),
117+
wantErrOut: regexp.MustCompile(`authentication failed`),
117118
},
118119
{
119120
name: "token set, missing scope",
@@ -124,7 +125,8 @@ func Test_statusRun(t *testing.T) {
124125
httpStubs: func(reg *httpmock.Registry) {
125126
reg.Register(httpmock.REST("GET", ""), httpmock.ScopesResponder("repo,"))
126127
},
127-
wantErr: regexp.MustCompile(`missing required scope 'read:org'`),
128+
wantErr: regexp.MustCompile(``),
129+
wantErrOut: regexp.MustCompile(`missing required scope 'read:org'`),
128130
},
129131
{
130132
name: "token set, good token",

0 commit comments

Comments
 (0)