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

Commit cf9e7e4

Browse files
feat: Option to list all workspaces (#416)
* Option to list all workspaces From an admin POV, this will be helpful for various reasons. * Update internal/cmd/workspaces.go Co-authored-by: Dean Sheather <dean@deansheather.com> * Fixes * Update internal/cmd/workspaces.go Co-authored-by: Dean Sheather <dean@deansheather.com> * Fix lint Co-authored-by: Dean Sheather <dean@deansheather.com>
1 parent 753937a commit cf9e7e4

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

internal/cmd/ceapi.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ func lookupUserOrgs(user *coder.User, orgs []coder.Organization) []coder.Organiz
3232
return userOrgs
3333
}
3434

35+
// getAllWorkspaces gets all workspaces for all users, on all providers.
36+
func getAllWorkspaces(ctx context.Context, client coder.Client) ([]coder.Workspace, error) {
37+
return client.Workspaces(ctx)
38+
}
39+
3540
// getWorkspaces returns all workspaces for the user.
3641
func getWorkspaces(ctx context.Context, client coder.Client, email string) ([]coder.Workspace, error) {
3742
user, err := client.UserByEmail(ctx, email)

internal/cmd/workspaces.go

Lines changed: 13 additions & 8 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,16 +86,19 @@ func lsWorkspacesCommand() *cobra.Command {
8586
if err != nil {
8687
return err
8788
}
88-
workspaces, err := getWorkspaces(ctx, client, user)
89+
90+
var workspaces []coder.Workspace
91+
switch {
92+
case all:
93+
workspaces, err = getAllWorkspaces(ctx, client)
94+
case provider != "":
95+
workspaces, err = getWorkspacesByProvider(ctx, client, provider, user)
96+
default:
97+
workspaces, err = getWorkspaces(ctx, client, user)
98+
}
8999
if err != nil {
90100
return err
91101
}
92-
if provider != "" {
93-
workspaces, err = getWorkspacesByProvider(ctx, client, provider, user)
94-
if err != nil {
95-
return err
96-
}
97-
}
98102
if len(workspaces) < 1 {
99103
clog.LogInfo("no workspaces found")
100104
workspaces = []coder.Workspace{} // ensures that json output still marshals
@@ -124,6 +128,7 @@ func lsWorkspacesCommand() *cobra.Command {
124128
},
125129
}
126130

131+
cmd.Flags().BoolVar(&all, "all", false, "Get workspaces for all users (admin only)")
127132
cmd.Flags().StringVar(&user, "user", coder.Me, "Specify the user whose resources to target")
128133
cmd.Flags().StringVarP(&outputFmt, "output", "o", humanOutput, "human | json")
129134
cmd.Flags().StringVarP(&provider, "provider", "p", "", "Filter workspaces by a particular workspace provider name.")
@@ -210,7 +215,7 @@ func (*wsPinger) logSuccess(timeStr, msg string) {
210215
fmt.Printf("%s: %s\n", color.New(color.Bold, color.FgGreen).Sprint(timeStr), msg)
211216
}
212217

213-
// Only return fatal errors
218+
// Only return fatal errors.
214219
func (w *wsPinger) ping(ctx context.Context) error {
215220
ctx, cancelFunc := context.WithTimeout(ctx, time.Second*15)
216221
defer cancelFunc()

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy