Path: blob/main/components/gitpod-cli/cmd/user-info.go
2498 views
// Copyright (c) 2024 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package cmd56import (7"context"8"encoding/json"9"fmt"10"os"11"time"1213"github.com/olekukonko/tablewriter"14log "github.com/sirupsen/logrus"15"github.com/spf13/cobra"16"golang.org/x/xerrors"1718"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"19serverapi "github.com/gitpod-io/gitpod/gitpod-protocol"20supervisorapi "github.com/gitpod-io/gitpod/supervisor/api"21)2223var userInfoCmdOpts struct {24// Json configures whether the command output is printed as JSON, to make it machine-readable.25Json bool2627// EmailOnly returns only the email address of the user28EmailOnly bool29}3031// infoCmd represents the info command.32var userInfoCmd = &cobra.Command{33Use: "user-info",34Short: "Display user info about the workspace owner, such as its ID, email, etc.",35RunE: func(cmd *cobra.Command, args []string) error {36ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)37defer cancel()3839client, err := connectToAPI(ctx)40if err != nil {41return err42}4344user, err := client.GetLoggedInUser(ctx)45if err != nil {46return err47}4849// determine which email to show: prefer SSO, with random as fallback50email := user.GetSSOEmail()51if email == "" {52email = user.GetRandomEmail()53}5455data := &userInfoData{56UserId: user.ID,57Email: email,58}5960if userInfoCmdOpts.EmailOnly {61fmt.Println(data.Email)62return nil63}6465if userInfoCmdOpts.Json {66content, _ := json.Marshal(data)67fmt.Println(string(content))68return nil69}70outputUserInfo(data)71return nil72},73}7475type userInfoData struct {76UserId string `json:"user_id"`77Email string `json:"email"`78}7980func outputUserInfo(info *userInfoData) {81table := tablewriter.NewWriter(os.Stdout)82table.SetColWidth(50)83table.SetBorder(false)84table.SetColumnSeparator(":")85table.Append([]string{"User ID", info.UserId})86table.Append([]string{"Email", info.Email})87table.Render()88}8990func connectToAPI(ctx context.Context) (*serverapi.APIoverJSONRPC, error) {91supervisorClient, err := supervisor.New(ctx)92if err != nil {93return nil, xerrors.Errorf("failed connecting to supervisor: %w", err)94}95defer supervisorClient.Close()9697wsinfo, err := supervisorClient.Info.WorkspaceInfo(ctx, &supervisorapi.WorkspaceInfoRequest{})98if err != nil {99return nil, xerrors.Errorf("failed getting workspace info from supervisor: %w", err)100}101102clientToken, err := supervisorClient.Token.GetToken(ctx, &supervisorapi.GetTokenRequest{103Host: wsinfo.GitpodApi.Host,104Kind: "gitpod",105Scope: []string{106"function:getLoggedInUser",107},108})109if err != nil {110return nil, xerrors.Errorf("failed getting token from supervisor: %w", err)111}112113serverLog := log.NewEntry(log.StandardLogger())114client, err := serverapi.ConnectToServer(wsinfo.GitpodApi.Endpoint, serverapi.ConnectToServerOpts{115Token: clientToken.Token,116Context: ctx,117Log: serverLog,118})119if err != nil {120return nil, xerrors.Errorf("failed connecting to server: %w", err)121}122return client, nil123}124125func init() {126userInfoCmd.Flags().BoolVarP(&userInfoCmdOpts.Json, "json", "j", false, "Output in JSON format")127userInfoCmd.Flags().BoolVar(&userInfoCmdOpts.EmailOnly, "email", false, "Only emit the email address of the user")128rootCmd.AddCommand(userInfoCmd)129}130131132