Skip to content

Commit 93f81fe

Browse files
authored
[gp] Introduce "gp user-info" showing userId and email (#20206)
* [gp] Introduce "gp user-info" showing userId and email * [gp] gp user-info: Add --email
1 parent feb6058 commit 93f81fe

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Copyright (c) 2024 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+
"encoding/json"
10+
"fmt"
11+
"os"
12+
"time"
13+
14+
"github.com/olekukonko/tablewriter"
15+
log "github.com/sirupsen/logrus"
16+
"github.com/spf13/cobra"
17+
"golang.org/x/xerrors"
18+
19+
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"
20+
serverapi "github.com/gitpod-io/gitpod/gitpod-protocol"
21+
supervisorapi "github.com/gitpod-io/gitpod/supervisor/api"
22+
)
23+
24+
var userInfoCmdOpts struct {
25+
// Json configures whether the command output is printed as JSON, to make it machine-readable.
26+
Json bool
27+
28+
// EmailOnly returns only the email address of the user
29+
EmailOnly bool
30+
}
31+
32+
// infoCmd represents the info command.
33+
var userInfoCmd = &cobra.Command{
34+
Use: "user-info",
35+
Short: "Display user info about the workspace owner, such as its ID, email, etc.",
36+
RunE: func(cmd *cobra.Command, args []string) error {
37+
ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)
38+
defer cancel()
39+
40+
client, err := connectToAPI(ctx)
41+
if err != nil {
42+
return err
43+
}
44+
45+
user, err := client.GetLoggedInUser(ctx)
46+
if err != nil {
47+
return err
48+
}
49+
50+
// determine which email to show: prefer SSO, with random as fallback
51+
email := user.GetSSOEmail()
52+
if email == "" {
53+
email = user.GetRandomEmail()
54+
}
55+
56+
data := &userInfoData{
57+
UserId: user.ID,
58+
Email: email,
59+
}
60+
61+
if userInfoCmdOpts.EmailOnly {
62+
fmt.Println(data.Email)
63+
return nil
64+
}
65+
66+
if userInfoCmdOpts.Json {
67+
content, _ := json.Marshal(data)
68+
fmt.Println(string(content))
69+
return nil
70+
}
71+
outputUserInfo(data)
72+
return nil
73+
},
74+
}
75+
76+
type userInfoData struct {
77+
UserId string `json:"user_id"`
78+
Email string `json:"email"`
79+
}
80+
81+
func outputUserInfo(info *userInfoData) {
82+
table := tablewriter.NewWriter(os.Stdout)
83+
table.SetColWidth(50)
84+
table.SetBorder(false)
85+
table.SetColumnSeparator(":")
86+
table.Append([]string{"User ID", info.UserId})
87+
table.Append([]string{"Email", info.Email})
88+
table.Render()
89+
}
90+
91+
func connectToAPI(ctx context.Context) (*serverapi.APIoverJSONRPC, error) {
92+
supervisorClient, err := supervisor.New(ctx)
93+
if err != nil {
94+
return nil, xerrors.Errorf("failed connecting to supervisor: %w", err)
95+
}
96+
defer supervisorClient.Close()
97+
98+
wsinfo, err := supervisorClient.Info.WorkspaceInfo(ctx, &supervisorapi.WorkspaceInfoRequest{})
99+
if err != nil {
100+
return nil, xerrors.Errorf("failed getting workspace info from supervisor: %w", err)
101+
}
102+
103+
clientToken, err := supervisorClient.Token.GetToken(ctx, &supervisorapi.GetTokenRequest{
104+
Host: wsinfo.GitpodApi.Host,
105+
Kind: "gitpod",
106+
Scope: []string{
107+
"function:getLoggedInUser",
108+
},
109+
})
110+
if err != nil {
111+
return nil, xerrors.Errorf("failed getting token from supervisor: %w", err)
112+
}
113+
114+
serverLog := log.NewEntry(log.StandardLogger())
115+
client, err := serverapi.ConnectToServer(wsinfo.GitpodApi.Endpoint, serverapi.ConnectToServerOpts{
116+
Token: clientToken.Token,
117+
Context: ctx,
118+
Log: serverLog,
119+
})
120+
if err != nil {
121+
return nil, xerrors.Errorf("failed connecting to server: %w", err)
122+
}
123+
return client, nil
124+
}
125+
126+
func init() {
127+
userInfoCmd.Flags().BoolVarP(&userInfoCmdOpts.Json, "json", "j", false, "Output in JSON format")
128+
userInfoCmd.Flags().BoolVar(&userInfoCmdOpts.EmailOnly, "email", false, "Only emit the email address of the user")
129+
rootCmd.AddCommand(userInfoCmd)
130+
}

0 commit comments

Comments
 (0)