Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/local-app/cmd/whoami.go
2497 views
1
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"context"
9
10
"github.com/bufbuild/connect-go"
11
"github.com/gitpod-io/gitpod/components/public-api/go/client"
12
v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1"
13
"github.com/gitpod-io/local-app/pkg/config"
14
"github.com/gitpod-io/local-app/pkg/prettyprint"
15
"github.com/spf13/cobra"
16
)
17
18
var whoamiCmd = &cobra.Command{
19
Use: "whoami",
20
Short: "Produces information about the current user",
21
RunE: func(cmd *cobra.Command, args []string) error {
22
cmd.SilenceUsage = true
23
24
gpctx, err := config.FromContext(cmd.Context()).GetActiveContext()
25
if err != nil {
26
return err
27
}
28
29
client, err := getGitpodClient(cmd.Context())
30
if err != nil {
31
return err
32
}
33
34
who, err := whoami(cmd.Context(), client, gpctx)
35
if err != nil {
36
return err
37
}
38
39
return WriteTabular(who, whoamiOpts.Format, prettyprint.WriterFormatNarrow)
40
},
41
}
42
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
65
type whoamiResult struct {
66
Name string `print:"user name"`
67
ID string `print:"user id"`
68
Org string `print:"organization"`
69
OrgID string `print:"organization id"`
70
Host string `print:"host"`
71
}
72
73
var whoamiOpts struct {
74
Format formatOpts
75
}
76
77
func init() {
78
rootCmd.AddCommand(whoamiCmd)
79
80
addFormatFlags(whoamiCmd, &whoamiOpts.Format)
81
}
82
83