diff --git a/internal/cli/auth/whoami.go b/internal/cli/auth/whoami.go index 372475c818..2082577021 100644 --- a/internal/cli/auth/whoami.go +++ b/internal/cli/auth/whoami.go @@ -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{} @@ -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 } diff --git a/internal/cli/auth/whoami_test.go b/internal/cli/auth/whoami_test.go index 7f5363b82c..7b148e4a9e 100644 --- a/internal/cli/auth/whoami_test.go +++ b/internal/cli/auth/whoami_test.go @@ -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: "test@test.com", + 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()) }