Skip to content

refactor(cli): extract workspace list parameters #10605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 9, 2023
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
63 changes: 63 additions & 0 deletions cli/cliui/filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cliui

import (
"github.com/coder/coder/v2/cli/clibase"
"github.com/coder/coder/v2/codersdk"
)

var defaultQuery = "owner:me"

// WorkspaceFilter wraps codersdk.WorkspaceFilter
// and allows easy integration to a CLI command.
// Example usage:
//
// func (r *RootCmd) MyCmd() *clibase.Cmd {
// var (
// filter cliui.WorkspaceFilter
// ...
// )
// cmd := &clibase.Cmd{
// ...
// }
// filter.AttachOptions(&cmd.Options)
// ...
// return cmd
// }
//
// The above will add the following flags to the command:
// --all
// --search
type WorkspaceFilter struct {
searchQuery string
all bool
}

func (w *WorkspaceFilter) Filter() codersdk.WorkspaceFilter {
var f codersdk.WorkspaceFilter
if w.all {
return f
}
f.FilterQuery = w.searchQuery
if f.FilterQuery == "" {
f.FilterQuery = defaultQuery
}
return f
}

func (w *WorkspaceFilter) AttachOptions(opts *clibase.OptionSet) {
*opts = append(*opts,
clibase.Option{
Flag: "all",
FlagShorthand: "a",
Description: "Specifies whether all workspaces will be listed or not.",

Value: clibase.BoolOf(&w.all),
},
clibase.Option{
Flag: "search",
Description: "Search for a workspace with a query.",
Default: defaultQuery,
Value: clibase.StringOf(&w.searchQuery),
},
)
}
30 changes: 3 additions & 27 deletions cli/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ func workspaceListRowFromWorkspace(now time.Time, usersByID map[uuid.UUID]coders

func (r *RootCmd) list() *clibase.Cmd {
var (
all bool
defaultQuery = "owner:me"
searchQuery string
filter cliui.WorkspaceFilter
displayWorkspaces []workspaceListRow
formatter = cliui.NewOutputFormatter(
cliui.TableFormat(
Expand Down Expand Up @@ -109,14 +107,7 @@ func (r *RootCmd) list() *clibase.Cmd {
r.InitClient(client),
),
Handler: func(inv *clibase.Invocation) error {
filter := codersdk.WorkspaceFilter{
FilterQuery: searchQuery,
}
if all && searchQuery == defaultQuery {
filter.FilterQuery = ""
}

res, err := client.Workspaces(inv.Context(), filter)
res, err := client.Workspaces(inv.Context(), filter.Filter())
if err != nil {
return err
}
Expand Down Expand Up @@ -153,22 +144,7 @@ func (r *RootCmd) list() *clibase.Cmd {
return err
},
}
cmd.Options = clibase.OptionSet{
{
Flag: "all",
FlagShorthand: "a",
Description: "Specifies whether all workspaces will be listed or not.",

Value: clibase.BoolOf(&all),
},
{
Flag: "search",
Description: "Search for a workspace with a query.",
Default: defaultQuery,
Value: clibase.StringOf(&searchQuery),
},
}

filter.AttachOptions(&cmd.Options)
formatter.AttachOptions(&cmd.Options)
return cmd
}