Skip to content

Commit

Permalink
CLOUDP-276968: improve whoami err message (#3299)
Browse files Browse the repository at this point in the history
Co-authored-by: Gustavo Bazan <[email protected]>
  • Loading branch information
tibulca and gssbzn authored Oct 7, 2024
1 parent e4f4ced commit a2f8afa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
24 changes: 19 additions & 5 deletions internal/cli/auth/whoami.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,39 @@ import (
)

type whoOpts struct {
OutWriter io.Writer
account string
OutWriter io.Writer
authSubject string
authType string
}

func (opts *whoOpts) Run() error {
_, _ = fmt.Fprintf(opts.OutWriter, "Logged in as %s\n", opts.account)
_, _ = fmt.Fprintf(opts.OutWriter, "Logged in as %s %s\n", opts.authSubject, opts.authType)

return nil
}

var ErrUnauthenticated = errors.New("not logged in")
var ErrUnauthenticated = errors.New("not logged in with an Atlas account or API key")

func AccountWithAccessToken() (string, error) {
if config.AccessToken() == "" {
return "", ErrUnauthenticated
}

return config.AccessTokenSubject()
}

func authTypeAndSubject() (string, string, error) {
if config.PublicAPIKey() != "" {
return "key", config.PublicAPIKey(), nil
}

if subject, err := AccountWithAccessToken(); err == nil {
return "account", subject, nil
}

return "", "", ErrUnauthenticated
}

func WhoAmIBuilder() *cobra.Command {
opts := &whoOpts{}

Expand All @@ -58,7 +72,7 @@ func WhoAmIBuilder() *cobra.Command {
},
RunE: func(_ *cobra.Command, _ []string) error {
var err error
if opts.account, err = AccountWithAccessToken(); err != nil {
if opts.authType, opts.authSubject, err = authTypeAndSubject(); err != nil {
return err
}

Expand Down
7 changes: 4 additions & 3 deletions internal/cli/auth/whoami_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ func TestWhoAmIBuilder(t *testing.T) {
func Test_whoOpts_Run(t *testing.T) {
buf := new(bytes.Buffer)
opts := &whoOpts{
OutWriter: buf,
account: "test",
OutWriter: buf,
authSubject: "[email protected]",
authType: "account",
}
require.NoError(t, opts.Run())
assert.Equal(t, "Logged in as test\n", buf.String())
assert.Equal(t, "Logged in as test@test.com account\n", buf.String())
}

0 comments on commit a2f8afa

Please sign in to comment.