- Notifications
You must be signed in to change notification settings - Fork928
feat:coder ls
should show possible columns to filter by#4240
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -2,6 +2,8 @@ package cli | ||
import ( | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
"time" | ||
"github.com/google/uuid" | ||
@@ -58,10 +60,12 @@ func workspaceListRowFromWorkspace(now time.Time, usersByID map[uuid.UUID]coders | ||
func list() *cobra.Command { | ||
var ( | ||
all bool | ||
columns []string | ||
defaultQuery = "owner:me" | ||
searchQuery string | ||
me bool | ||
displayWorkspaces []workspaceListRow | ||
) | ||
cmd := &cobra.Command{ | ||
Annotations: workspaceCommand, | ||
@@ -80,6 +84,14 @@ func list() *cobra.Command { | ||
if all && searchQuery == defaultQuery { | ||
filter.FilterQuery = "" | ||
} | ||
if me { | ||
myUser, err := client.User(cmd.Context(), codersdk.Me) | ||
if err != nil { | ||
return err | ||
} | ||
filter.Owner = myUser.Username | ||
} | ||
Comment on lines +88 to +94 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. review: this might be a left-over piece of a merge conflict | ||
workspaces, err := client.Workspaces(cmd.Context(), filter) | ||
if err != nil { | ||
return err | ||
@@ -101,7 +113,7 @@ func list() *cobra.Command { | ||
} | ||
now := time.Now() | ||
displayWorkspaces = make([]workspaceListRow, len(workspaces)) | ||
for i, workspace := range workspaces { | ||
displayWorkspaces[i] = workspaceListRowFromWorkspace(now, usersByID, workspace) | ||
} | ||
@@ -115,10 +127,21 @@ func list() *cobra.Command { | ||
return err | ||
}, | ||
} | ||
v := reflect.Indirect(reflect.ValueOf(displayWorkspaces)) | ||
availColumns, err := cliui.TypeToTableHeaders(v.Type().Elem()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
for i, s := range availColumns { | ||
availColumns[i] = strings.Replace(s, " ", "_", -1) | ||
} | ||
columnString := strings.Join(availColumns[:], ", ") | ||
Comment on lines +131 to +139 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. This is good, but since we'd probably want to use this in multiple commands it would be better served as a function in the cliui package that accepts funcTableHeaders(tany) ([]string,error) {v:=reflect.Indirect(reflect.ValueOf(t))returncliui.TypeToTableHeaders(v.Type())}funcFormatColumnNames(columns []string)string {out:=strings.Join(columns[:],", ")out=strings.ReplaceAll(out," ","_")returnout} which could be called like columns,err:=cliui.TableHeaders(workspaceListRow{})iferr!=nil {panic(err)}columnString:=cliui.FormatColumnNames(columns) This would also remove the need for having a global(ish) var for the output There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. future fix ideaaa :D | ||
cmd.Flags().BoolVarP(&all, "all", "a", false, | ||
"Specifies whether all workspaces will be listed or not.") | ||
cmd.Flags().StringArrayVarP(&columns, "column", "c", nil, | ||
fmt.Sprintf("Specify a column to filter in the table. Available columns are: %v", columnString)) | ||
cmd.Flags().StringVar(&searchQuery, "search","", "Search for a workspace with a query.") | ||
return cmd | ||
} |