Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-cli/cmd/info.go
2498 views
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
"os"
12
"time"
13
14
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod"
15
"github.com/gitpod-io/gitpod/supervisor/api"
16
"github.com/olekukonko/tablewriter"
17
"github.com/spf13/cobra"
18
)
19
20
var infoCmdOpts struct {
21
// Json configures whether the command output is printed as JSON, to make it machine-readable.
22
Json bool
23
}
24
25
// infoCmd represents the info command.
26
var infoCmd = &cobra.Command{
27
Use: "info",
28
Short: "Display workspace info, such as its ID, class, etc.",
29
RunE: func(cmd *cobra.Command, args []string) error {
30
ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)
31
defer cancel()
32
33
wsInfo, err := gitpod.GetWSInfo(ctx)
34
if err != nil {
35
return err
36
}
37
38
data := &infoData{
39
WorkspaceId: wsInfo.WorkspaceId,
40
InstanceId: wsInfo.InstanceId,
41
WorkspaceClass: wsInfo.WorkspaceClass,
42
WorkspaceUrl: wsInfo.WorkspaceUrl,
43
WorkspaceContextUrl: wsInfo.WorkspaceContextUrl,
44
ClusterHost: wsInfo.WorkspaceClusterHost,
45
}
46
47
if infoCmdOpts.Json {
48
content, _ := json.Marshal(data)
49
fmt.Println(string(content))
50
return nil
51
}
52
outputInfo(data)
53
return nil
54
},
55
}
56
57
type infoData struct {
58
WorkspaceId string `json:"workspace_id"`
59
InstanceId string `json:"instance_id"`
60
WorkspaceClass *api.WorkspaceInfoResponse_WorkspaceClass `json:"workspace_class"`
61
WorkspaceUrl string `json:"workspace_url"`
62
WorkspaceContextUrl string `json:"workspace_context_url"`
63
ClusterHost string `json:"cluster_host"`
64
}
65
66
func outputInfo(info *infoData) {
67
table := tablewriter.NewWriter(os.Stdout)
68
table.SetColWidth(50)
69
table.SetBorder(false)
70
table.SetColumnSeparator(":")
71
table.Append([]string{"Workspace ID", info.WorkspaceId})
72
table.Append([]string{"Instance ID", info.InstanceId})
73
table.Append([]string{"Workspace class", fmt.Sprintf("%s: %s", info.WorkspaceClass.DisplayName, info.WorkspaceClass.Description)})
74
table.Append([]string{"Workspace URL", info.WorkspaceUrl})
75
table.Append([]string{"Workspace Context URL", info.WorkspaceContextUrl})
76
table.Append([]string{"Cluster host", info.ClusterHost})
77
table.Render()
78
}
79
80
func init() {
81
infoCmd.Flags().BoolVarP(&infoCmdOpts.Json, "json", "j", false, "Output in JSON format")
82
rootCmd.AddCommand(infoCmd)
83
}
84
85