Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/dev/gpctl/cmd/workspaces-list.go
2498 views
1
// Copyright (c) 2020 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
"fmt"
10
11
"github.com/spf13/cobra"
12
13
"github.com/gitpod-io/gitpod/common-go/log"
14
"github.com/gitpod-io/gitpod/ws-manager/api"
15
)
16
17
// workspacesListCmd represents the describe command
18
var workspacesListCmd = &cobra.Command{
19
Use: "list",
20
Short: "lists all workspaces",
21
Run: func(cmd *cobra.Command, args []string) {
22
ctx, cancel := context.WithCancel(context.Background())
23
defer cancel()
24
25
conn, client, err := getWorkspacesClient(ctx)
26
if err != nil {
27
log.WithError(err).Fatal("cannot connect")
28
}
29
defer conn.Close()
30
31
resp, err := client.GetWorkspaces(ctx, &api.GetWorkspacesRequest{})
32
if err != nil {
33
log.WithError(err).Fatal("error during RPC call")
34
}
35
36
type PrintWorkspace struct {
37
Owner string
38
WorkspaceID string
39
Instance string
40
Phase string
41
Type string
42
Pod string
43
Active bool
44
Node string
45
}
46
47
var out []PrintWorkspace
48
for _, w := range resp.Status {
49
pod := "unknown"
50
switch w.GetSpec().GetType() {
51
case api.WorkspaceType_REGULAR:
52
pod = fmt.Sprintf("ws-%s", w.GetId())
53
case api.WorkspaceType_PREBUILD:
54
pod = fmt.Sprintf("prebuild-%s", w.GetId())
55
case api.WorkspaceType_IMAGEBUILD:
56
pod = fmt.Sprintf("imagebuild-%s", w.GetId())
57
}
58
59
var nodeName string
60
if w.Runtime != nil {
61
nodeName = w.Runtime.NodeName
62
}
63
out = append(out, PrintWorkspace{
64
Owner: w.GetMetadata().GetOwner(),
65
WorkspaceID: w.GetMetadata().GetMetaId(),
66
Instance: w.GetId(),
67
Phase: w.GetPhase().String(),
68
Type: w.GetSpec().GetType().String(),
69
Pod: pod,
70
Active: w.GetConditions().FirstUserActivity != nil,
71
Node: nodeName,
72
})
73
}
74
75
tpl := `OWNER WORKSPACE INSTANCE PHASE TYPE POD ACTIVE NODE
76
{{- range . }}
77
{{ .Owner }} {{ .WorkspaceID }} {{ .Instance }} {{ .Phase }} {{ .Type }} {{ .Pod }} {{ .Active }} {{ .Node -}}
78
{{ end }}
79
`
80
err = getOutputFormat(tpl, "{.id}").Print(out)
81
if err != nil {
82
log.Fatal(err)
83
}
84
},
85
}
86
87
func init() {
88
workspacesCmd.AddCommand(workspacesListCmd)
89
}
90
91