Skip to content

Commit 0c75ea6

Browse files
endingwithaliAli Diamond
andauthored
feat: coder ls should show possible columns to filter by (#4240)
* added showing columns in help call, need to format to make pretty * finished formatting column strings for print of list /ls command Co-authored-by: Ali Diamond <user@ali.dev>
1 parent df7c739 commit 0c75ea6

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

cli/cliui/table.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func DisplayTable(out any, sort string, filterColumns []string) (string, error)
6767
}
6868

6969
// Get the list of table column headers.
70-
headersRaw, err := typeToTableHeaders(v.Type().Elem())
70+
headersRaw, err := TypeToTableHeaders(v.Type().Elem())
7171
if err != nil {
7272
return "", xerrors.Errorf("get table headers recursively for type %q: %w", v.Type().Elem().String(), err)
7373
}
@@ -207,10 +207,10 @@ func isStructOrStructPointer(t reflect.Type) bool {
207207
return t.Kind() == reflect.Struct || (t.Kind() == reflect.Pointer && t.Elem().Kind() == reflect.Struct)
208208
}
209209

210-
// typeToTableHeaders converts a type to a slice of column names. If the given
210+
// TypeToTableHeaders converts a type to a slice of column names. If the given
211211
// type is invalid (not a struct or a pointer to a struct, has invalid table
212212
// tags, etc.), an error is returned.
213-
func typeToTableHeaders(t reflect.Type) ([]string, error) {
213+
func TypeToTableHeaders(t reflect.Type) ([]string, error) {
214214
if !isStructOrStructPointer(t) {
215215
return nil, xerrors.Errorf("typeToTableHeaders called with a non-struct or a non-pointer-to-a-struct type")
216216
}
@@ -235,7 +235,7 @@ func typeToTableHeaders(t reflect.Type) ([]string, error) {
235235
return nil, xerrors.Errorf("field %q in type %q is marked as recursive but does not contain a struct or a pointer to a struct", field.Name, t.String())
236236
}
237237

238-
childNames, err := typeToTableHeaders(fieldType)
238+
childNames, err := TypeToTableHeaders(fieldType)
239239
if err != nil {
240240
return nil, xerrors.Errorf("get child field header names for field %q in type %q: %w", field.Name, fieldType.String(), err)
241241
}

cli/list.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package cli
22

33
import (
44
"fmt"
5+
"reflect"
6+
"strings"
57
"time"
68

79
"github.com/google/uuid"
@@ -58,10 +60,12 @@ func workspaceListRowFromWorkspace(now time.Time, usersByID map[uuid.UUID]coders
5860

5961
func list() *cobra.Command {
6062
var (
61-
all bool
62-
columns []string
63-
defaultQuery = "owner:me"
64-
searchQuery string
63+
all bool
64+
columns []string
65+
defaultQuery = "owner:me"
66+
searchQuery string
67+
me bool
68+
displayWorkspaces []workspaceListRow
6569
)
6670
cmd := &cobra.Command{
6771
Annotations: workspaceCommand,
@@ -80,6 +84,14 @@ func list() *cobra.Command {
8084
if all && searchQuery == defaultQuery {
8185
filter.FilterQuery = ""
8286
}
87+
88+
if me {
89+
myUser, err := client.User(cmd.Context(), codersdk.Me)
90+
if err != nil {
91+
return err
92+
}
93+
filter.Owner = myUser.Username
94+
}
8395
workspaces, err := client.Workspaces(cmd.Context(), filter)
8496
if err != nil {
8597
return err
@@ -101,7 +113,7 @@ func list() *cobra.Command {
101113
}
102114

103115
now := time.Now()
104-
displayWorkspaces := make([]workspaceListRow, len(workspaces))
116+
displayWorkspaces = make([]workspaceListRow, len(workspaces))
105117
for i, workspace := range workspaces {
106118
displayWorkspaces[i] = workspaceListRowFromWorkspace(now, usersByID, workspace)
107119
}
@@ -115,10 +127,21 @@ func list() *cobra.Command {
115127
return err
116128
},
117129
}
130+
131+
v := reflect.Indirect(reflect.ValueOf(displayWorkspaces))
132+
availColumns, err := cliui.TypeToTableHeaders(v.Type().Elem())
133+
if err != nil {
134+
panic(err)
135+
}
136+
for i, s := range availColumns {
137+
availColumns[i] = strings.Replace(s, " ", "_", -1)
138+
}
139+
columnString := strings.Join(availColumns[:], ", ")
140+
118141
cmd.Flags().BoolVarP(&all, "all", "a", false,
119142
"Specifies whether all workspaces will be listed or not.")
120143
cmd.Flags().StringArrayVarP(&columns, "column", "c", nil,
121-
"Specify a column to filter in the table.")
122-
cmd.Flags().StringVar(&searchQuery, "search", defaultQuery, "Search for a workspace with a query.")
144+
fmt.Sprintf("Specify a column to filter in the table. Available columns are: %v", columnString))
145+
cmd.Flags().StringVar(&searchQuery, "search", "", "Search for a workspace with a query.")
123146
return cmd
124147
}

0 commit comments

Comments
 (0)