Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit fc3336c

Browse files
committed
Option to list all workspaces
From an admin POV, this will be helpful for various reasons.
1 parent 3ebd102 commit fc3336c

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

internal/cmd/ceapi.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,13 @@ func getWorkspacesByProvider(ctx context.Context, client coder.Client, wpName, u
215215
return nil, err
216216
}
217217

218-
workspaces, err = filterWorkspacesByUser(ctx, client, userEmail, workspaces)
219-
if err != nil {
220-
return nil, err
218+
if userEmail != "" {
219+
workspaces, err = filterWorkspacesByUser(ctx, client, userEmail, workspaces)
220+
if err != nil {
221+
return nil, err
222+
}
221223
}
224+
222225
return workspaces, nil
223226
}
224227

internal/cmd/workspaces.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ const (
7070

7171
func lsWorkspacesCommand() *cobra.Command {
7272
var (
73+
all bool
7374
outputFmt string
7475
user string
7576
provider string
@@ -85,11 +86,18 @@ func lsWorkspacesCommand() *cobra.Command {
8586
if err != nil {
8687
return err
8788
}
88-
workspaces, err := getWorkspaces(ctx, client, user)
89-
if err != nil {
90-
return err
89+
var workspaces []coder.Workspace
90+
if !all {
91+
var err error
92+
workspaces, err = getWorkspaces(ctx, client, user)
93+
if err != nil {
94+
return err
95+
}
96+
} else {
97+
// If the user gave the all flag, then filtering by user doesn't make sense.
98+
user = ""
9199
}
92-
if provider != "" {
100+
if provider != "" || all {
93101
workspaces, err = getWorkspacesByProvider(ctx, client, provider, user)
94102
if err != nil {
95103
return err
@@ -124,6 +132,7 @@ func lsWorkspacesCommand() *cobra.Command {
124132
},
125133
}
126134

135+
cmd.Flags().BoolVar(&all, "all", false, "Get workspaces for all users")
127136
cmd.Flags().StringVar(&user, "user", coder.Me, "Specify the user whose resources to target")
128137
cmd.Flags().StringVarP(&outputFmt, "output", "o", humanOutput, "human | json")
129138
cmd.Flags().StringVarP(&provider, "provider", "p", "", "Filter workspaces by a particular workspace provider name.")

internal/cmd/workspaces_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,34 @@ func Test_workspaces_ls(t *testing.T) {
2929
res.stdoutUnmarshals(t, &workspaces)
3030
}
3131

32+
func Test_workspaces_ls_all(t *testing.T) {
33+
skipIfNoAuth(t)
34+
for _, test := range []struct {
35+
name string
36+
command []string
37+
assert func(r result)
38+
}{
39+
{
40+
name: "simple list",
41+
command: []string{"workspaces", "ls", "--all"},
42+
assert: func(r result) { r.success(t) },
43+
},
44+
{
45+
name: "list as json",
46+
command: []string{"workspaces", "ls", "--all", "--output", "json"},
47+
assert: func(r result) {
48+
var workspaces []coder.Workspace
49+
r.stdoutUnmarshals(t, &workspaces)
50+
},
51+
},
52+
} {
53+
test := test
54+
t.Run(test.name, func(t *testing.T) {
55+
test.assert(execute(t, nil, test.command...))
56+
})
57+
}
58+
}
59+
3260
func Test_workspaces_ls_by_provider(t *testing.T) {
3361
skipIfNoAuth(t)
3462
for _, test := range []struct {

0 commit comments

Comments
 (0)