Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print information after login #19029

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading