|
| 1 | +// Copyright (c) 2022 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 | + "github.com/gitpod-io/gitpod/common-go/log" |
| 12 | + supervisor_helper "github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor-helper" |
| 13 | + supervisor "github.com/gitpod-io/gitpod/supervisor/api" |
| 14 | + "github.com/olekukonko/tablewriter" |
| 15 | + "github.com/spf13/cobra" |
| 16 | + "google.golang.org/grpc" |
| 17 | + "os" |
| 18 | + "time" |
| 19 | +) |
| 20 | + |
| 21 | +var infoCmdOpts struct { |
| 22 | + // Json configures whether the command output is printed as JSON, to make it machine-readable. |
| 23 | + Json bool |
| 24 | +} |
| 25 | + |
| 26 | +// infoCmd represents the info command. |
| 27 | +var infoCmd = &cobra.Command{ |
| 28 | + Use: "info", |
| 29 | + Short: "Display workspace info, such as its ID, class, etc.", |
| 30 | + Run: func(cmd *cobra.Command, args []string) { |
| 31 | + ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second) |
| 32 | + defer cancel() |
| 33 | + |
| 34 | + conn, err := supervisor_helper.Dial(ctx) |
| 35 | + if err != nil { |
| 36 | + log.Fatal(err) |
| 37 | + } |
| 38 | + defer conn.Close() |
| 39 | + |
| 40 | + data, err := fetchInfo(ctx, conn) |
| 41 | + if err != nil { |
| 42 | + log.Fatal(err) |
| 43 | + } |
| 44 | + |
| 45 | + if infoCmdOpts.Json { |
| 46 | + content, _ := json.Marshal(data) |
| 47 | + fmt.Println(string(content)) |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + outputInfo(data) |
| 52 | + }, |
| 53 | +} |
| 54 | + |
| 55 | +func fetchInfo(ctx context.Context, conn *grpc.ClientConn) (*infoData, error) { |
| 56 | + wsInfo, err := supervisor.NewInfoServiceClient(conn).WorkspaceInfo(ctx, &supervisor.WorkspaceInfoRequest{}) |
| 57 | + if err != nil { |
| 58 | + return nil, fmt.Errorf("failed to retrieve workspace info: %w", err) |
| 59 | + } |
| 60 | + |
| 61 | + return &infoData{ |
| 62 | + WorkspaceId: wsInfo.WorkspaceId, |
| 63 | + InstanceId: wsInfo.InstanceId, |
| 64 | + WorkspaceClass: wsInfo.WorkspaceClass, |
| 65 | + WorkspaceUrl: wsInfo.WorkspaceUrl, |
| 66 | + ClusterHost: wsInfo.WorkspaceClusterHost, |
| 67 | + }, nil |
| 68 | +} |
| 69 | + |
| 70 | +func outputInfo(info *infoData) { |
| 71 | + table := tablewriter.NewWriter(os.Stdout) |
| 72 | + table.SetColWidth(50) |
| 73 | + table.SetBorder(false) |
| 74 | + table.SetColumnSeparator(":") |
| 75 | + table.Append([]string{"Workspace ID", info.WorkspaceId}) |
| 76 | + table.Append([]string{"Instance ID", info.InstanceId}) |
| 77 | + table.Append([]string{"Workspace class", fmt.Sprintf("%s: %s", info.WorkspaceClass.DisplayName, info.WorkspaceClass.Description)}) |
| 78 | + table.Append([]string{"Workspace URL", info.WorkspaceUrl}) |
| 79 | + table.Append([]string{"Cluster host", info.ClusterHost}) |
| 80 | + table.Render() |
| 81 | +} |
| 82 | + |
| 83 | +type infoData struct { |
| 84 | + WorkspaceId string `json:"workspace_id"` |
| 85 | + InstanceId string `json:"instance_id"` |
| 86 | + WorkspaceClass *supervisor.WorkspaceInfoResponse_WorkspaceClass `json:"workspace_class"` |
| 87 | + WorkspaceUrl string `json:"workspace_url"` |
| 88 | + ClusterHost string `json:"cluster_host"` |
| 89 | +} |
| 90 | + |
| 91 | +func init() { |
| 92 | + infoCmd.Flags().BoolVarP(&infoCmdOpts.Json, "json", "j", false, "Output in JSON format") |
| 93 | + rootCmd.AddCommand(infoCmd) |
| 94 | +} |
0 commit comments