From 92a902e45303696e3fa162ec6715988ce915e341 Mon Sep 17 00:00:00 2001 From: Sam Coe Date: Mon, 4 Dec 2023 14:40:32 -0400 Subject: [PATCH] Add context to auth token command error message --- pkg/cmd/auth/token/token.go | 10 ++++++++-- pkg/cmd/auth/token/token_test.go | 22 ++++++++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/pkg/cmd/auth/token/token.go b/pkg/cmd/auth/token/token.go index 2c57771b6..a582684a8 100644 --- a/pkg/cmd/auth/token/token.go +++ b/pkg/cmd/auth/token/token.go @@ -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 } diff --git a/pkg/cmd/auth/token/token_test.go b/pkg/cmd/auth/token/token_test.go index dc9968b31..6e9e246af 100644 --- a/pkg/cmd/auth/token/token_test.go +++ b/pkg/cmd/auth/token/token_test.go @@ -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",