Skip to content

Commit

Permalink
Print information after login (#19029)
Browse files Browse the repository at this point in the history
* Print information after login

* Adress review feedback
  • Loading branch information
csweichel authored Nov 7, 2023
1 parent d7eae21 commit 7dfb884
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
19 changes: 17 additions & 2 deletions components/local-app/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ 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)
if slog.Default().Enabled(cmd.Context(), slog.LevelDebug) {
slog.Debug("could not write token to keyring, storing in config file instead", "err", err)
} else {
slog.Warn("could not write token to keyring, storing in config file instead. Use -v to see the error.")
}
gpctx.Token = token
}

Expand Down Expand Up @@ -115,7 +119,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)
},
}

Expand Down
41 changes: 27 additions & 14 deletions components/local-app/cmd/whoami.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
},
}

// whoami returns 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"`
Expand Down

0 comments on commit 7dfb884

Please sign in to comment.