Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
endingwithali merged 2 commits intomainfromendingwithali/column-search-feature
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes fromall 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
8 changes: 4 additions & 4 deletionscli/cliui/table.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -67,7 +67,7 @@ func DisplayTable(out any, sort string, filterColumns []string) (string, error)
}

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

//typeToTableHeaders converts a type to a slice of column names. If the given
//TypeToTableHeaders converts a type to a slice of column names. If the given
// type is invalid (not a struct or a pointer to a struct, has invalid table
// tags, etc.), an error is returned.
functypeToTableHeaders(t reflect.Type) ([]string, error) {
funcTypeToTableHeaders(t reflect.Type) ([]string, error) {
if !isStructOrStructPointer(t) {
return nil, xerrors.Errorf("typeToTableHeaders called with a non-struct or a non-pointer-to-a-struct type")
}
Expand All@@ -235,7 +235,7 @@ func typeToTableHeaders(t reflect.Type) ([]string, error) {
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())
}

childNames, err :=typeToTableHeaders(fieldType)
childNames, err :=TypeToTableHeaders(fieldType)
if err != nil {
return nil, xerrors.Errorf("get child field header names for field %q in type %q: %w", field.Name, fieldType.String(), err)
}
Expand Down
37 changes: 30 additions & 7 deletionscli/list.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,8 @@ package cli

import (
"fmt"
"reflect"
"strings"
"time"

"github.com/google/uuid"
Expand DownExpand Up@@ -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
all bool
columns []string
defaultQuery = "owner:me"
searchQuery string
me bool
displayWorkspaces []workspaceListRow
)
cmd := &cobra.Command{
Annotations: workspaceCommand,
Expand All@@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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
Expand All@@ -101,7 +113,7 @@ func list() *cobra.Command {
}

now := time.Now()
displayWorkspaces:= make([]workspaceListRow, len(workspaces))
displayWorkspaces = make([]workspaceListRow, len(workspaces))
for i, workspace := range workspaces {
displayWorkspaces[i] = workspaceListRowFromWorkspace(now, usersByID, workspace)
}
Expand All@@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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 acceptsany:

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

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The 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,
"Specify a column to filter in the table.")
cmd.Flags().StringVar(&searchQuery, "search",defaultQuery, "Search for a workspace with a query.")
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
}

[8]ページ先頭

©2009-2025 Movatter.jp