-
Notifications
You must be signed in to change notification settings - Fork 928
Use new table formatter everywhere #3544
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,12 +5,10 @@ import ( | |
"fmt" | ||
"strings" | ||
|
||
"github.com/coder/coder/cli/cliui" | ||
"github.com/jedib0t/go-pretty/v6/table" | ||
|
||
"github.com/spf13/cobra" | ||
"golang.org/x/xerrors" | ||
|
||
"github.com/coder/coder/cli/cliui" | ||
"github.com/coder/coder/codersdk" | ||
) | ||
|
||
|
@@ -38,10 +36,6 @@ func featuresList() *cobra.Command { | |
Use: "list", | ||
Aliases: []string{"ls"}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
err := cliui.ValidateColumns(featureColumns, columns) | ||
if err != nil { | ||
return err | ||
} | ||
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. One downside of this change is that we have to round-trip to the server for a response instead of exiting immediately on bad input. Would it make sense to expose a 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. I'm hoping to automate this whole process of validating columns and adding the columns/sort flags in a future PR. I just removed this because it didn't match any of the other commands, but your comment is valid. I can add it back if you want or we could wait until we have better output utilities. 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. Sounds good. My opinion is that we’re not in a hurry to restore the early check. It’s a small QoL improvement but it can wait for the refactor just fine. |
||
client, err := createClient(cmd) | ||
if err != nil { | ||
return err | ||
|
@@ -54,11 +48,14 @@ func featuresList() *cobra.Command { | |
out := "" | ||
switch outputFormat { | ||
case "table", "": | ||
out = displayFeatures(columns, entitlements.Features) | ||
out, err = displayFeatures(columns, entitlements.Features) | ||
if err != nil { | ||
return xerrors.Errorf("render table: %w", err) | ||
} | ||
case "json": | ||
outBytes, err := json.Marshal(entitlements) | ||
if err != nil { | ||
return xerrors.Errorf("marshal users to JSON: %w", err) | ||
return xerrors.Errorf("marshal features to JSON: %w", err) | ||
} | ||
|
||
out = string(outBytes) | ||
|
@@ -78,35 +75,28 @@ func featuresList() *cobra.Command { | |
return cmd | ||
} | ||
|
||
type featureRow struct { | ||
Name string `table:"name"` | ||
Entitlement string `table:"entitlement"` | ||
Enabled bool `table:"enabled"` | ||
Limit *int64 `table:"limit"` | ||
Actual *int64 `table:"actual"` | ||
} | ||
|
||
// displayFeatures will return a table displaying all features passed in. | ||
// filterColumns must be a subset of the feature fields and will determine which | ||
// columns to display | ||
func displayFeatures(filterColumns []string, features map[string]codersdk.Feature) string { | ||
tableWriter := cliui.Table() | ||
header := table.Row{} | ||
for _, h := range featureColumns { | ||
header = append(header, h) | ||
} | ||
tableWriter.AppendHeader(header) | ||
tableWriter.SetColumnConfigs(cliui.FilterTableColumns(header, filterColumns)) | ||
tableWriter.SortBy([]table.SortBy{{ | ||
Name: "username", | ||
}}) | ||
func displayFeatures(filterColumns []string, features map[string]codersdk.Feature) (string, error) { | ||
rows := make([]featureRow, 0, len(features)) | ||
for name, feat := range features { | ||
tableWriter.AppendRow(table.Row{ | ||
name, | ||
feat.Entitlement, | ||
feat.Enabled, | ||
intOrNil(feat.Limit), | ||
intOrNil(feat.Actual), | ||
rows = append(rows, featureRow{ | ||
Name: name, | ||
Entitlement: string(feat.Entitlement), | ||
Enabled: feat.Enabled, | ||
Limit: feat.Limit, | ||
Actual: feat.Actual, | ||
}) | ||
} | ||
return tableWriter.Render() | ||
} | ||
|
||
func intOrNil(i *int64) string { | ||
if i == nil { | ||
return "" | ||
} | ||
return fmt.Sprintf("%d", *i) | ||
return cliui.DisplayTable(rows, "name", filterColumns) | ||
} |
Uh oh!
There was an error while loading. Please reload this page.