Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions internal/cmd/ceapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ func lookupUserOrgs(user *coder.User, orgs []coder.Organization) []coder.Organiz
return userOrgs
}

// getAllWorkspaces gets all workspaces for all users, on all providers.
func getAllWorkspaces(ctx context.Context, client coder.Client) ([]coder.Workspace, error) {
return client.Workspaces(ctx)
}

// getWorkspaces returns all workspaces for the user.
func getWorkspaces(ctx context.Context, client coder.Client, email string) ([]coder.Workspace, error) {
user, err := client.UserByEmail(ctx, email)
Expand Down
22 changes: 17 additions & 5 deletions internal/cmd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const (

func lsWorkspacesCommand() *cobra.Command {
var (
all bool
outputFmt string
user string
provider string
Expand All @@ -85,15 +86,25 @@ func lsWorkspacesCommand() *cobra.Command {
if err != nil {
return err
}
workspaces, err := getWorkspaces(ctx, client, user)
if err != nil {
return err
}
if provider != "" {
var workspaces []coder.Workspace
if all {
var err error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't need the var errs here or in the other if branches since it's already defined above, then you can condense the if err check to be underneath the if branches, so each branch is just the workspaces, err = getX() statement.

edit: check my suggestion

workspaces, err = getAllWorkspaces(ctx, client)
if err != nil {
return err
}
} else if provider != "" {
var err error
workspaces, err = getWorkspacesByProvider(ctx, client, provider, user)
if err != nil {
return err
}
} else {
var err error
workspaces, err = getWorkspaces(ctx, client, user)
if err != nil {
return err
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shuffled this around because it seemed like the first fetch for the user-only workspaces was ignored if providers was also passed.

if len(workspaces) < 1 {
clog.LogInfo("no workspaces found")
Expand Down Expand Up @@ -124,6 +135,7 @@ func lsWorkspacesCommand() *cobra.Command {
},
}

cmd.Flags().BoolVar(&all, "all", false, "Get workspaces for all users (admin only)")
cmd.Flags().StringVar(&user, "user", coder.Me, "Specify the user whose resources to target")
cmd.Flags().StringVarP(&outputFmt, "output", "o", humanOutput, "human | json")
cmd.Flags().StringVarP(&provider, "provider", "p", "", "Filter workspaces by a particular workspace provider name.")
Expand Down
28 changes: 28 additions & 0 deletions internal/cmd/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,34 @@ func Test_workspaces_ls(t *testing.T) {
res.stdoutUnmarshals(t, &workspaces)
}

func Test_workspaces_ls_all(t *testing.T) {
skipIfNoAuth(t)
for _, test := range []struct {
name string
command []string
assert func(r result)
}{
{
name: "simple list",
command: []string{"workspaces", "ls", "--all"},
assert: func(r result) { r.success(t) },
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sanity check: This isn't actually checking the output - just that the command exited with 0?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Essentially, yeah. This mini test framework "runs" the commands in-process and checks if the command fn returned an error.

},
{
name: "list as json",
command: []string{"workspaces", "ls", "--all", "--output", "json"},
assert: func(r result) {
var workspaces []coder.Workspace
r.stdoutUnmarshals(t, &workspaces)
},
},
} {
test := test
t.Run(test.name, func(t *testing.T) {
test.assert(execute(t, nil, test.command...))
})
}
}

func Test_workspaces_ls_by_provider(t *testing.T) {
skipIfNoAuth(t)
for _, test := range []struct {
Expand Down