Add context to auth token command error message

This commit is contained in:
Sam Coe 2023-12-04 14:40:32 -04:00 committed by William Martin
parent af8bcd3ed2
commit 92a902e453
2 changed files with 28 additions and 4 deletions

View file

@ -33,7 +33,7 @@ func NewCmdToken(f *cmdutil.Factory, runF func(*TokenOptions) error) *cobra.Comm
Without the %[1]s--hostname%[1]s flag, the default host is chosen.
Without the %[1]s--user%[1]s flag, the currently active account for the host is chosen.
Without the %[1]s--user%[1]s flag, the active account for the host is chosen.
`, "`"),
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
@ -82,12 +82,18 @@ func tokenRun(opts *TokenOptions) error {
val, _, _ = authCfg.TokenForUser(hostname, opts.Username)
}
}
if val == "" {
return fmt.Errorf("no oauth token")
errMsg := fmt.Sprintf("no oauth token found for %s", hostname)
if opts.Username != "" {
errMsg += fmt.Sprintf(" account %s", opts.Username)
}
return fmt.Errorf(errMsg)
}
if val != "" {
fmt.Fprintf(opts.IO.Out, "%s\n", val)
}
return nil
}

View file

@ -124,7 +124,16 @@ func TestTokenRun(t *testing.T) {
name: "no token",
opts: TokenOptions{},
wantErr: true,
wantErrMsg: "no oauth token",
wantErrMsg: "no oauth token found for github.com",
},
{
name: "no token for hostname user",
opts: TokenOptions{
Hostname: "ghe.io",
Username: "test-user",
},
wantErr: true,
wantErrMsg: "no oauth token found for ghe.io account test-user",
},
{
name: "uses default host when one is not provided",
@ -211,7 +220,16 @@ func TestTokenRunSecureStorage(t *testing.T) {
name: "no token",
opts: TokenOptions{},
wantErr: true,
wantErrMsg: "no oauth token",
wantErrMsg: "no oauth token found for github.com",
},
{
name: "no token for hostname user",
opts: TokenOptions{
Hostname: "ghe.io",
Username: "test-user",
},
wantErr: true,
wantErrMsg: "no oauth token found for ghe.io account test-user",
},
{
name: "token for user",