From cffb2f490b983469f3b20457473da4e591148561 Mon Sep 17 00:00:00 2001 From: "Christian Weichel (Chris)" Date: Tue, 7 Nov 2023 15:59:36 +0000 Subject: [PATCH] Print information after login --- components/local-app/cmd/login.go | 16 ++++++++++-- components/local-app/cmd/whoami.go | 41 ++++++++++++++++++++---------- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/components/local-app/cmd/login.go b/components/local-app/cmd/login.go index 5ee43740e822b3..3344b274f55393 100644 --- a/components/local-app/cmd/login.go +++ b/components/local-app/cmd/login.go @@ -66,7 +66,8 @@ var loginCmd = &cobra.Command{ err = auth.SetToken(loginOpts.Host, token) if err != nil { - slog.Warn("could not write token to keyring, storing in config file instead", "err", err) + slog.Debug("could not write token to keyring, storing in config file instead") + slog.Warn("could not write token to keyring, storing in config file instead. Use -v to see the error.") gpctx.Token = token } @@ -115,7 +116,18 @@ var loginCmd = &cobra.Command{ return err } - return nil + client, err := getGitpodClient(config.ToContext(cmd.Context(), cfg)) + if err != nil { + return err + } + who, err := whoami(cmd.Context(), client, gpctx) + if err != nil { + return err + } + + slog.Info("Login succesfull") + fmt.Println() + return WriteTabular(who, formatOpts{}, prettyprint.WriterFormatNarrow) }, } diff --git a/components/local-app/cmd/whoami.go b/components/local-app/cmd/whoami.go index d61ad5ffec0d4a..a9e61f3860e4b2 100644 --- a/components/local-app/cmd/whoami.go +++ b/components/local-app/cmd/whoami.go @@ -5,7 +5,10 @@ package cmd import ( + "context" + "github.com/bufbuild/connect-go" + "github.com/gitpod-io/gitpod/components/public-api/go/client" v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1" "github.com/gitpod-io/local-app/pkg/config" "github.com/gitpod-io/local-app/pkg/prettyprint" @@ -28,27 +31,37 @@ var whoamiCmd = &cobra.Command{ return err } - user, err := client.User.GetAuthenticatedUser(cmd.Context(), &connect.Request[v1.GetAuthenticatedUserRequest]{}) - if err != nil { - return err - } - org, err := client.Teams.GetTeam(cmd.Context(), &connect.Request[v1.GetTeamRequest]{Msg: &v1.GetTeamRequest{TeamId: gpctx.OrganizationID}}) + who, err := whoami(cmd.Context(), client, gpctx) if err != nil { return err } - return WriteTabular([]whoamiResult{ - { - Name: user.Msg.GetUser().Name, - ID: user.Msg.GetUser().Id, - Org: org.Msg.GetTeam().Name, - OrgID: org.Msg.GetTeam().Id, - Host: gpctx.Host.String(), - }, - }, whoamiOpts.Format, prettyprint.WriterFormatNarrow) + return WriteTabular(who, whoamiOpts.Format, prettyprint.WriterFormatNarrow) }, } +// printWhoami prints information about the currently logged in user +func whoami(ctx context.Context, client *client.Gitpod, gpctx *config.ConnectionContext) ([]whoamiResult, error) { + user, err := client.User.GetAuthenticatedUser(ctx, &connect.Request[v1.GetAuthenticatedUserRequest]{}) + if err != nil { + return nil, err + } + org, err := client.Teams.GetTeam(ctx, &connect.Request[v1.GetTeamRequest]{Msg: &v1.GetTeamRequest{TeamId: gpctx.OrganizationID}}) + if err != nil { + return nil, err + } + + return []whoamiResult{ + { + Name: user.Msg.GetUser().Name, + ID: user.Msg.GetUser().Id, + Org: org.Msg.GetTeam().Name, + OrgID: org.Msg.GetTeam().Id, + Host: gpctx.Host.String(), + }, + }, nil +} + type whoamiResult struct { Name string `print:"user name"` ID string `print:"user id"`