Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
/coder-v1-cliPublic archive

Commitcf9e7e4

Browse files
feat: Option to list all workspaces (#416)
* Option to list all workspacesFrom an admin POV, this will be helpful for various reasons.* Update internal/cmd/workspaces.goCo-authored-by: Dean Sheather <dean@deansheather.com>* Fixes* Update internal/cmd/workspaces.goCo-authored-by: Dean Sheather <dean@deansheather.com>* Fix lintCo-authored-by: Dean Sheather <dean@deansheather.com>
1 parent753937a commitcf9e7e4

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
returnuserOrgs
3333
}
3434

35+
// getAllWorkspaces gets all workspaces for all users, on all providers.
36+
funcgetAllWorkspaces(ctx context.Context,client coder.Client) ([]coder.Workspace,error) {
37+
returnclient.Workspaces(ctx)
38+
}
39+
3540
// getWorkspaces returns all workspaces for the user.
3641
funcgetWorkspaces(ctx context.Context,client coder.Client,emailstring) ([]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
funclsWorkspacesCommand()*cobra.Command {
7272
var (
73+
allbool
7374
outputFmtstring
7475
userstring
7576
providerstring
@@ -85,16 +86,19 @@ func lsWorkspacesCommand() *cobra.Command {
8586
iferr!=nil {
8687
returnerr
8788
}
88-
workspaces,err:=getWorkspaces(ctx,client,user)
89+
90+
varworkspaces []coder.Workspace
91+
switch {
92+
caseall:
93+
workspaces,err=getAllWorkspaces(ctx,client)
94+
caseprovider!="":
95+
workspaces,err=getWorkspacesByProvider(ctx,client,provider,user)
96+
default:
97+
workspaces,err=getWorkspaces(ctx,client,user)
98+
}
8999
iferr!=nil {
90100
returnerr
91101
}
92-
ifprovider!="" {
93-
workspaces,err=getWorkspacesByProvider(ctx,client,provider,user)
94-
iferr!=nil {
95-
returnerr
96-
}
97-
}
98102
iflen(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
defercancelFunc()

‎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+
funcTest_workspaces_ls_all(t*testing.T) {
33+
skipIfNoAuth(t)
34+
for_,test:=range []struct {
35+
namestring
36+
command []string
37+
assertfunc(rresult)
38+
}{
39+
{
40+
name:"simple list",
41+
command: []string{"workspaces","ls","--all"},
42+
assert:func(rresult) {r.success(t) },
43+
},
44+
{
45+
name:"list as json",
46+
command: []string{"workspaces","ls","--all","--output","json"},
47+
assert:func(rresult) {
48+
varworkspaces []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
funcTest_workspaces_ls_by_provider(t*testing.T) {
3361
skipIfNoAuth(t)
3462
for_,test:=range []struct {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp