Skip to content

Commit 7dfb884

Browse files
authored
Print information after login (#19029)
* Print information after login * Adress review feedback
1 parent d7eae21 commit 7dfb884

File tree

2 files changed

+44
-16
lines changed

2 files changed

+44
-16
lines changed

components/local-app/cmd/login.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ var loginCmd = &cobra.Command{
6666

6767
err = auth.SetToken(loginOpts.Host, token)
6868
if err != nil {
69-
slog.Warn("could not write token to keyring, storing in config file instead", "err", err)
69+
if slog.Default().Enabled(cmd.Context(), slog.LevelDebug) {
70+
slog.Debug("could not write token to keyring, storing in config file instead", "err", err)
71+
} else {
72+
slog.Warn("could not write token to keyring, storing in config file instead. Use -v to see the error.")
73+
}
7074
gpctx.Token = token
7175
}
7276

@@ -115,7 +119,18 @@ var loginCmd = &cobra.Command{
115119
return err
116120
}
117121

118-
return nil
122+
client, err := getGitpodClient(config.ToContext(cmd.Context(), cfg))
123+
if err != nil {
124+
return err
125+
}
126+
who, err := whoami(cmd.Context(), client, gpctx)
127+
if err != nil {
128+
return err
129+
}
130+
131+
slog.Info("Login succesfull")
132+
fmt.Println()
133+
return WriteTabular(who, formatOpts{}, prettyprint.WriterFormatNarrow)
119134
},
120135
}
121136

components/local-app/cmd/whoami.go

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
package cmd
66

77
import (
8+
"context"
9+
810
"github.com/bufbuild/connect-go"
11+
"github.com/gitpod-io/gitpod/components/public-api/go/client"
912
v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1"
1013
"github.com/gitpod-io/local-app/pkg/config"
1114
"github.com/gitpod-io/local-app/pkg/prettyprint"
@@ -28,27 +31,37 @@ var whoamiCmd = &cobra.Command{
2831
return err
2932
}
3033

31-
user, err := client.User.GetAuthenticatedUser(cmd.Context(), &connect.Request[v1.GetAuthenticatedUserRequest]{})
32-
if err != nil {
33-
return err
34-
}
35-
org, err := client.Teams.GetTeam(cmd.Context(), &connect.Request[v1.GetTeamRequest]{Msg: &v1.GetTeamRequest{TeamId: gpctx.OrganizationID}})
34+
who, err := whoami(cmd.Context(), client, gpctx)
3635
if err != nil {
3736
return err
3837
}
3938

40-
return WriteTabular([]whoamiResult{
41-
{
42-
Name: user.Msg.GetUser().Name,
43-
ID: user.Msg.GetUser().Id,
44-
Org: org.Msg.GetTeam().Name,
45-
OrgID: org.Msg.GetTeam().Id,
46-
Host: gpctx.Host.String(),
47-
},
48-
}, whoamiOpts.Format, prettyprint.WriterFormatNarrow)
39+
return WriteTabular(who, whoamiOpts.Format, prettyprint.WriterFormatNarrow)
4940
},
5041
}
5142

43+
// whoami returns information about the currently logged in user
44+
func whoami(ctx context.Context, client *client.Gitpod, gpctx *config.ConnectionContext) ([]whoamiResult, error) {
45+
user, err := client.User.GetAuthenticatedUser(ctx, &connect.Request[v1.GetAuthenticatedUserRequest]{})
46+
if err != nil {
47+
return nil, err
48+
}
49+
org, err := client.Teams.GetTeam(ctx, &connect.Request[v1.GetTeamRequest]{Msg: &v1.GetTeamRequest{TeamId: gpctx.OrganizationID}})
50+
if err != nil {
51+
return nil, err
52+
}
53+
54+
return []whoamiResult{
55+
{
56+
Name: user.Msg.GetUser().Name,
57+
ID: user.Msg.GetUser().Id,
58+
Org: org.Msg.GetTeam().Name,
59+
OrgID: org.Msg.GetTeam().Id,
60+
Host: gpctx.Host.String(),
61+
},
62+
}, nil
63+
}
64+
5265
type whoamiResult struct {
5366
Name string `print:"user name"`
5467
ID string `print:"user id"`

0 commit comments

Comments
 (0)