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

feat: Option to list all workspaces #416

Merged
merged 5 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 13 additions & 8 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,16 +86,19 @@ func lsWorkspacesCommand() *cobra.Command {
if err != nil {
return err
}
workspaces, err := getWorkspaces(ctx, client, user)

var workspaces []coder.Workspace
switch {
case all:
workspaces, err = getAllWorkspaces(ctx, client)
case provider != "":
workspaces, err = getWorkspacesByProvider(ctx, client, provider, user)
default:
workspaces, err = getWorkspaces(ctx, client, user)
}
if err != nil {
return err
}
if provider != "" {
workspaces, err = getWorkspacesByProvider(ctx, client, provider, user)
if err != nil {
return err
}
}
if len(workspaces) < 1 {
clog.LogInfo("no workspaces found")
workspaces = []coder.Workspace{} // ensures that json output still marshals
Expand Down Expand Up @@ -124,6 +128,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 Expand Up @@ -210,7 +215,7 @@ func (*wsPinger) logSuccess(timeStr, msg string) {
fmt.Printf("%s: %s\n", color.New(color.Bold, color.FgGreen).Sprint(timeStr), msg)
}

// Only return fatal errors
// Only return fatal errors.
func (w *wsPinger) ping(ctx context.Context) error {
ctx, cancelFunc := context.WithTimeout(ctx, time.Second*15)
defer cancelFunc()
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