Skip to content

Commit

Permalink
Print information after login
Browse files Browse the repository at this point in the history
  • Loading branch information
csweichel committed Nov 7, 2023
1 parent cf78ae0 commit cffb2f4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
16 changes: 14 additions & 2 deletions components/local-app/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

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

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)
},
}

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

0 comments on commit cffb2f4

Please sign in to comment.