- Notifications
You must be signed in to change notification settings - Fork928
fix: Match kubectl table style for simpler scripting#1363
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package cliui | ||
import ( | ||
"strings" | ||
"github.com/jedib0t/go-pretty/v6/table" | ||
) | ||
// Table creates a new table with standardized styles. | ||
func Table() table.Writer { | ||
tableWriter := table.NewWriter() | ||
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. Not to bikeshed on this, but Go had a stdlib solution for this:https://pkg.go.dev/text/tabwriter. Haven't taken a look at this package though 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. Oh actually nvm, I thought u were totally switching out the table packages 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. Oh, nah. This package handles hiding columns and sorting which is kinda nice. 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. ye i agree, should've looked through more b4 commenting | ||
tableWriter.Style().Box.PaddingLeft = "" | ||
tableWriter.Style().Box.PaddingRight = " " | ||
tableWriter.Style().Options.DrawBorder = false | ||
tableWriter.Style().Options.SeparateHeader = false | ||
tableWriter.Style().Options.SeparateColumns = false | ||
return tableWriter | ||
} | ||
// FilterTableColumns returns configurations to hide columns | ||
// that are not provided in the array. If the array is empty, | ||
// no filtering will occur! | ||
func FilterTableColumns(header table.Row, columns []string) []table.ColumnConfig { | ||
if len(columns) == 0 { | ||
return nil | ||
} | ||
columnConfigs := make([]table.ColumnConfig, 0) | ||
for _, headerTextRaw := range header { | ||
headerText, _ := headerTextRaw.(string) | ||
hidden := true | ||
for _, column := range columns { | ||
if strings.EqualFold(strings.ReplaceAll(column, "_", " "), headerText) { | ||
hidden = false | ||
break | ||
} | ||
} | ||
columnConfigs = append(columnConfigs, table.ColumnConfig{ | ||
Name: headerText, | ||
Hidden: hidden, | ||
}) | ||
} | ||
return columnConfigs | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package cli_test | ||
import ( | ||
"testing" | ||
"github.com/stretchr/testify/require" | ||
"github.com/coder/coder/cli/clitest" | ||
"github.com/coder/coder/coderd/coderdtest" | ||
"github.com/coder/coder/pty/ptytest" | ||
) | ||
func TestList(t *testing.T) { | ||
t.Parallel() | ||
t.Run("Single", func(t *testing.T) { | ||
t.Parallel() | ||
client := coderdtest.New(t, nil) | ||
user := coderdtest.CreateFirstUser(t, client) | ||
coderdtest.NewProvisionerDaemon(t, client) | ||
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) | ||
coderdtest.AwaitTemplateVersionJob(t, client, version.ID) | ||
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID) | ||
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID) | ||
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID) | ||
cmd, root := clitest.New(t, "ls") | ||
clitest.SetupConfig(t, client, root) | ||
doneChan := make(chan struct{}) | ||
pty := ptytest.New(t) | ||
cmd.SetIn(pty.Input()) | ||
cmd.SetOut(pty.Output()) | ||
go func() { | ||
defer close(doneChan) | ||
err := cmd.Execute() | ||
require.NoError(t, err) | ||
}() | ||
pty.ExpectMatch(workspace.Name) | ||
pty.ExpectMatch("Running") | ||
<-doneChan | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -2,17 +2,20 @@ package cli | ||
import ( | ||
"fmt" | ||
"time" | ||
"github.com/jedib0t/go-pretty/v6/table" | ||
"github.com/spf13/cobra" | ||
"github.com/coder/coder/cli/cliui" | ||
"github.com/coder/coder/codersdk" | ||
) | ||
func userList() *cobra.Command { | ||
var ( | ||
columns []string | ||
) | ||
cmd := &cobra.Command{ | ||
Use: "list", | ||
Aliases: []string{"ls"}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
@@ -24,14 +27,14 @@ func userList() *cobra.Command { | ||
if err != nil { | ||
return err | ||
} | ||
tableWriter := cliui.Table() | ||
header := table.Row{"Username", "Email", "Created At"} | ||
tableWriter.AppendHeader(header) | ||
tableWriter.SetColumnConfigs(cliui.FilterTableColumns(header, columns)) | ||
tableWriter.SortBy([]table.SortBy{{ | ||
Name: "Username", | ||
}}) | ||
for _, user := range users { | ||
tableWriter.AppendRow(table.Row{ | ||
user.Username, | ||
@@ -43,4 +46,7 @@ func userList() *cobra.Command { | ||
return err | ||
}, | ||
} | ||
cmd.Flags().StringArrayVarP(&columns, "column", "c", nil, | ||
"Specify a column to filter in the table.") | ||
Comment on lines +49 to +50 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. To filter by multiple columns, do you need to specify the flag multiple times? Or can it accept a comma delimited string? I think we should document how to specify multiple. 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. You have to specify multiple, but the flag does specify the type as doing so. | ||
return cmd | ||
} |