Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/local-app/cmd/workspace-list.go
2497 views
1
// Copyright (c) 2023 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
"time"
11
12
"github.com/bufbuild/connect-go"
13
v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1"
14
"github.com/gitpod-io/local-app/pkg/config"
15
"github.com/gitpod-io/local-app/pkg/helper"
16
"github.com/gitpod-io/local-app/pkg/prettyprint"
17
"github.com/sagikazarmark/slog-shim"
18
"github.com/spf13/cobra"
19
)
20
21
// workspaceListCmd lists all available workspaces
22
var workspaceListCmd = &cobra.Command{
23
Use: "list",
24
Short: "Lists workspaces",
25
Aliases: []string{"ls"},
26
RunE: func(cmd *cobra.Command, args []string) error {
27
cmd.SilenceUsage = true
28
29
ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)
30
defer cancel()
31
32
gitpod, err := getGitpodClient(ctx)
33
if err != nil {
34
return err
35
}
36
37
cfg := config.FromContext(ctx)
38
gpctx, err := cfg.GetActiveContext()
39
if err != nil {
40
return err
41
}
42
orgId := gpctx.OrganizationID
43
44
workspaces, err := gitpod.Workspaces.ListWorkspaces(ctx, connect.NewRequest(&v1.ListWorkspacesRequest{
45
OrganizationId: orgId,
46
}))
47
if err != nil {
48
return err
49
}
50
51
result := make([]tabularWorkspace, 0, len(workspaces.Msg.GetResult()))
52
for _, ws := range workspaces.Msg.GetResult() {
53
r := newTabularWorkspace(ws)
54
if r == nil {
55
continue
56
}
57
if workspaceListOpts.RunningOnly && ws.Status.Instance.Status.Phase != v1.WorkspaceInstanceStatus_PHASE_RUNNING {
58
continue
59
}
60
result = append(result, *r)
61
}
62
63
return WriteTabular(result, workspaceListOpts.Format, prettyprint.WriterFormatWide)
64
},
65
}
66
67
func newTabularWorkspace(ws *v1.Workspace) *tabularWorkspace {
68
if !helper.HasInstanceStatus(ws) {
69
slog.Debug("workspace has no instance status - removing from output", "workspace", ws.WorkspaceId)
70
return nil
71
}
72
73
var repo string
74
wsDetails := ws.Context.GetDetails()
75
switch d := wsDetails.(type) {
76
case *v1.WorkspaceContext_Git_:
77
repo = fmt.Sprintf("%s/%s", d.Git.Repository.Owner, d.Git.Repository.Name)
78
case *v1.WorkspaceContext_Prebuild_:
79
repo = fmt.Sprintf("%s/%s", d.Prebuild.OriginalContext.Repository.Owner, d.Prebuild.OriginalContext.Repository.Name)
80
}
81
var branch string
82
if ws.Status.Instance.Status.GitStatus != nil {
83
branch = ws.Status.Instance.Status.GitStatus.Branch
84
if branch == "" || branch == "(detached)" {
85
branch = ""
86
}
87
}
88
return &tabularWorkspace{
89
ID: ws.WorkspaceId,
90
Repository: repo,
91
Branch: branch,
92
Status: prettyprint.FormatWorkspacePhase(ws.Status.Instance.Status.Phase),
93
}
94
}
95
96
type tabularWorkspace struct {
97
ID string `print:"id"`
98
Repository string `print:"repository"`
99
Branch string `print:"branch"`
100
Status string `print:"status"`
101
}
102
103
var workspaceListOpts struct {
104
Format formatOpts
105
RunningOnly bool
106
}
107
108
func init() {
109
workspaceCmd.AddCommand(workspaceListCmd)
110
addFormatFlags(workspaceListCmd, &workspaceListOpts.Format)
111
workspaceListCmd.Flags().BoolVarP(&workspaceListOpts.RunningOnly, "running-only", "r", false, "Only list running workspaces")
112
}
113
114