Use new table formatter everywhere (#3544)

This commit is contained in:
Dean Sheather
2022-08-19 02:41:00 +10:00
committed by GitHub
parent c43297937b
commit 3610402cd8
13 changed files with 199 additions and 198 deletions

View File

@ -90,7 +90,7 @@ func DisplayTable(out any, sort string, filterColumns []string) (string, error)
sort = strings.ToLower(strings.ReplaceAll(sort, "_", " "))
h, ok := headersMap[sort]
if !ok {
return "", xerrors.Errorf("specified sort column %q not found in table headers, available columns are %q", sort, strings.Join(headersRaw, `", "`))
return "", xerrors.Errorf(`specified sort column %q not found in table headers, available columns are "%v"`, sort, strings.Join(headersRaw, `", "`))
}
// Autocorrect
@ -101,7 +101,7 @@ func DisplayTable(out any, sort string, filterColumns []string) (string, error)
column := strings.ToLower(strings.ReplaceAll(column, "_", " "))
h, ok := headersMap[column]
if !ok {
return "", xerrors.Errorf("specified filter column %q not found in table headers, available columns are %q", sort, strings.Join(headersRaw, `", "`))
return "", xerrors.Errorf(`specified filter column %q not found in table headers, available columns are "%v"`, sort, strings.Join(headersRaw, `", "`))
}
// Autocorrect
@ -158,6 +158,10 @@ func DisplayTable(out any, sort string, filterColumns []string) (string, error)
if val != nil {
v = val.Format(time.Stamp)
}
case fmt.Stringer:
if val != nil {
v = val.String()
}
}
rowSlice[i] = v
@ -301,19 +305,3 @@ func valueToTableMap(val reflect.Value) (map[string]any, error) {
return row, nil
}
func ValidateColumns(all, given []string) error {
for _, col := range given {
found := false
for _, c := range all {
if strings.EqualFold(strings.ReplaceAll(col, "_", " "), c) {
found = true
break
}
}
if !found {
return fmt.Errorf("unknown column: %s", col)
}
}
return nil
}

View File

@ -1,6 +1,7 @@
package cliui_test
import (
"fmt"
"log"
"strings"
"testing"
@ -12,6 +13,16 @@ import (
"github.com/coder/coder/cli/cliui"
)
type stringWrapper struct {
str string
}
var _ fmt.Stringer = stringWrapper{}
func (s stringWrapper) String() string {
return s.str
}
type tableTest1 struct {
Name string `table:"name"`
NotIncluded string // no table tag
@ -28,7 +39,7 @@ type tableTest1 struct {
}
type tableTest2 struct {
Name string `table:"name"`
Name stringWrapper `table:"name"`
Age int `table:"age"`
NotIncluded string `table:"-"`
}
@ -48,21 +59,21 @@ func Test_DisplayTable(t *testing.T) {
Age: 10,
Roles: []string{"a", "b", "c"},
Sub1: tableTest2{
Name: "foo1",
Name: stringWrapper{str: "foo1"},
Age: 11,
},
Sub2: &tableTest2{
Name: "foo2",
Name: stringWrapper{str: "foo2"},
Age: 12,
},
Sub3: tableTest3{
Sub: tableTest2{
Name: "foo3",
Name: stringWrapper{str: "foo3"},
Age: 13,
},
},
Sub4: tableTest2{
Name: "foo4",
Name: stringWrapper{str: "foo4"},
Age: 14,
},
Time: someTime,
@ -73,18 +84,18 @@ func Test_DisplayTable(t *testing.T) {
Age: 20,
Roles: []string{"a"},
Sub1: tableTest2{
Name: "bar1",
Name: stringWrapper{str: "bar1"},
Age: 21,
},
Sub2: nil,
Sub3: tableTest3{
Sub: tableTest2{
Name: "bar3",
Name: stringWrapper{str: "bar3"},
Age: 23,
},
},
Sub4: tableTest2{
Name: "bar4",
Name: stringWrapper{str: "bar4"},
Age: 24,
},
Time: someTime,
@ -95,18 +106,18 @@ func Test_DisplayTable(t *testing.T) {
Age: 30,
Roles: nil,
Sub1: tableTest2{
Name: "baz1",
Name: stringWrapper{str: "baz1"},
Age: 31,
},
Sub2: nil,
Sub3: tableTest3{
Sub: tableTest2{
Name: "baz3",
Name: stringWrapper{str: "baz3"},
Age: 33,
},
},
Sub4: tableTest2{
Name: "baz4",
Name: stringWrapper{str: "baz4"},
Age: 34,
},
Time: someTime,

View File

@ -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
}
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)
}

View File

@ -5,7 +5,6 @@ import (
"time"
"github.com/google/uuid"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
"github.com/coder/coder/cli/cliui"
@ -14,6 +13,49 @@ import (
"github.com/coder/coder/codersdk"
)
type workspaceListRow struct {
Workspace string `table:"workspace"`
Template string `table:"template"`
Status string `table:"status"`
LastBuilt string `table:"last built"`
Outdated bool `table:"outdated"`
StartsAt string `table:"starts at"`
StopsAfter string `table:"stops after"`
}
func workspaceListRowFromWorkspace(now time.Time, usersByID map[uuid.UUID]codersdk.User, workspace codersdk.Workspace) workspaceListRow {
status := codersdk.WorkspaceDisplayStatus(workspace.LatestBuild.Job.Status, workspace.LatestBuild.Transition)
lastBuilt := now.UTC().Sub(workspace.LatestBuild.Job.CreatedAt).Truncate(time.Second)
autostartDisplay := "-"
if !ptr.NilOrEmpty(workspace.AutostartSchedule) {
if sched, err := schedule.Weekly(*workspace.AutostartSchedule); err == nil {
autostartDisplay = fmt.Sprintf("%s %s (%s)", sched.Time(), sched.DaysOfWeek(), sched.Location())
}
}
autostopDisplay := "-"
if !ptr.NilOrZero(workspace.TTLMillis) {
dur := time.Duration(*workspace.TTLMillis) * time.Millisecond
autostopDisplay = durationDisplay(dur)
if !workspace.LatestBuild.Deadline.IsZero() && workspace.LatestBuild.Deadline.After(now) && status == "Running" {
remaining := time.Until(workspace.LatestBuild.Deadline)
autostopDisplay = fmt.Sprintf("%s (%s)", autostopDisplay, relative(remaining))
}
}
user := usersByID[workspace.OwnerID]
return workspaceListRow{
Workspace: user.Username + "/" + workspace.Name,
Template: workspace.TemplateName,
Status: status,
LastBuilt: durationDisplay(lastBuilt),
Outdated: workspace.Outdated,
StartsAt: autostartDisplay,
StopsAfter: autostopDisplay,
}
}
func list() *cobra.Command {
var columns []string
cmd := &cobra.Command{
@ -32,10 +74,10 @@ func list() *cobra.Command {
return err
}
if len(workspaces) == 0 {
_, _ = fmt.Fprintln(cmd.OutOrStdout(), cliui.Styles.Prompt.String()+"No workspaces found! Create one:")
_, _ = fmt.Fprintln(cmd.OutOrStdout())
_, _ = fmt.Fprintln(cmd.OutOrStdout(), " "+cliui.Styles.Code.Render("coder create <name>"))
_, _ = fmt.Fprintln(cmd.OutOrStdout())
_, _ = fmt.Fprintln(cmd.ErrOrStderr(), cliui.Styles.Prompt.String()+"No workspaces found! Create one:")
_, _ = fmt.Fprintln(cmd.ErrOrStderr())
_, _ = fmt.Fprintln(cmd.ErrOrStderr(), " "+cliui.Styles.Code.Render("coder create <name>"))
_, _ = fmt.Fprintln(cmd.ErrOrStderr())
return nil
}
users, err := client.Users(cmd.Context(), codersdk.UsersRequest{})
@ -47,48 +89,18 @@ func list() *cobra.Command {
usersByID[user.ID] = user
}
tableWriter := cliui.Table()
header := table.Row{"workspace", "template", "status", "last built", "outdated", "starts at", "stops after"}
tableWriter.AppendHeader(header)
tableWriter.SortBy([]table.SortBy{{
Name: "workspace",
}})
tableWriter.SetColumnConfigs(cliui.FilterTableColumns(header, columns))
now := time.Now()
for _, workspace := range workspaces {
status := codersdk.WorkspaceDisplayStatus(workspace.LatestBuild.Job.Status, workspace.LatestBuild.Transition)
lastBuilt := time.Now().UTC().Sub(workspace.LatestBuild.Job.CreatedAt).Truncate(time.Second)
autostartDisplay := "-"
if !ptr.NilOrEmpty(workspace.AutostartSchedule) {
if sched, err := schedule.Weekly(*workspace.AutostartSchedule); err == nil {
autostartDisplay = fmt.Sprintf("%s %s (%s)", sched.Time(), sched.DaysOfWeek(), sched.Location())
}
displayWorkspaces := make([]workspaceListRow, len(workspaces))
for i, workspace := range workspaces {
displayWorkspaces[i] = workspaceListRowFromWorkspace(now, usersByID, workspace)
}
autostopDisplay := "-"
if !ptr.NilOrZero(workspace.TTLMillis) {
dur := time.Duration(*workspace.TTLMillis) * time.Millisecond
autostopDisplay = durationDisplay(dur)
if !workspace.LatestBuild.Deadline.IsZero() && workspace.LatestBuild.Deadline.After(now) && status == "Running" {
remaining := time.Until(workspace.LatestBuild.Deadline)
autostopDisplay = fmt.Sprintf("%s (%s)", autostopDisplay, relative(remaining))
}
out, err := cliui.DisplayTable(displayWorkspaces, "workspace", columns)
if err != nil {
return err
}
user := usersByID[workspace.OwnerID]
tableWriter.AppendRow(table.Row{
user.Username + "/" + workspace.Name,
workspace.TemplateName,
status,
durationDisplay(lastBuilt),
workspace.Outdated,
autostartDisplay,
autostopDisplay,
})
}
_, err = fmt.Fprintln(cmd.OutOrStdout(), tableWriter.Render())
_, err = fmt.Fprintln(cmd.OutOrStdout(), out)
return err
},
}

View File

@ -1,11 +1,7 @@
package cli
import (
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/codersdk"
)
func parameters() *cobra.Command {
@ -30,29 +26,3 @@ func parameters() *cobra.Command {
)
return cmd
}
// displayParameters will return a table displaying all parameters passed in.
// filterColumns must be a subset of the parameter fields and will determine which
// columns to display
func displayParameters(filterColumns []string, params ...codersdk.Parameter) string {
tableWriter := cliui.Table()
header := table.Row{"id", "scope", "scope id", "name", "source scheme", "destination scheme", "created at", "updated at"}
tableWriter.AppendHeader(header)
tableWriter.SetColumnConfigs(cliui.FilterTableColumns(header, filterColumns))
tableWriter.SortBy([]table.SortBy{{
Name: "name",
}})
for _, param := range params {
tableWriter.AppendRow(table.Row{
param.ID.String(),
param.Scope,
param.ScopeID.String(),
param.Name,
param.SourceScheme,
param.DestinationScheme,
param.CreatedAt,
param.UpdatedAt,
})
}
return tableWriter.Render()
}

View File

@ -7,6 +7,7 @@ import (
"github.com/spf13/cobra"
"golang.org/x/xerrors"
"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/codersdk"
)
@ -70,11 +71,16 @@ func parameterList() *cobra.Command {
return xerrors.Errorf("fetch params: %w", err)
}
_, err = fmt.Fprintln(cmd.OutOrStdout(), displayParameters(columns, params...))
out, err := cliui.DisplayTable(params, "name", columns)
if err != nil {
return xerrors.Errorf("render table: %w", err)
}
_, err = fmt.Fprintln(cmd.OutOrStdout(), out)
return err
},
}
cmd.Flags().StringArrayVarP(&columns, "column", "c", []string{"name", "scope", "destination_scheme"},
cmd.Flags().StringArrayVarP(&columns, "column", "c", []string{"name", "scope", "destination scheme"},
"Specify a column to filter in the table.")
return cmd
}

View File

@ -30,12 +30,17 @@ func templateList() *cobra.Command {
}
if len(templates) == 0 {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s No templates found in %s! Create one:\n\n", caret, color.HiWhiteString(organization.Name))
_, _ = fmt.Fprintln(cmd.OutOrStdout(), color.HiMagentaString(" $ coder templates create <directory>\n"))
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "%s No templates found in %s! Create one:\n\n", caret, color.HiWhiteString(organization.Name))
_, _ = fmt.Fprintln(cmd.ErrOrStderr(), color.HiMagentaString(" $ coder templates create <directory>\n"))
return nil
}
_, err = fmt.Fprintln(cmd.OutOrStdout(), displayTemplates(columns, templates...))
out, err := displayTemplates(columns, templates...)
if err != nil {
return err
}
_, err = fmt.Fprintln(cmd.OutOrStdout(), out)
return err
},
}

View File

@ -57,7 +57,7 @@ func TestTemplateList(t *testing.T) {
pty := ptytest.New(t)
cmd.SetIn(pty.Input())
cmd.SetOut(pty.Output())
cmd.SetErr(pty.Output())
errC := make(chan error)
go func() {

View File

@ -4,7 +4,7 @@ import (
"fmt"
"time"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/google/uuid"
"github.com/spf13/cobra"
"github.com/coder/coder/cli/cliui"
@ -46,35 +46,41 @@ func templates() *cobra.Command {
return cmd
}
type templateTableRow struct {
Name string `table:"name"`
CreatedAt string `table:"created at"`
LastUpdated string `table:"last updated"`
OrganizationID uuid.UUID `table:"organization id"`
Provisioner codersdk.ProvisionerType `table:"provisioner"`
ActiveVersionID uuid.UUID `table:"active version id"`
UsedBy string `table:"used by"`
MaxTTL time.Duration `table:"max ttl"`
MinAutostartInterval time.Duration `table:"min autostart"`
}
// displayTemplates will return a table displaying all templates passed in.
// filterColumns must be a subset of the template fields and will determine which
// columns to display
func displayTemplates(filterColumns []string, templates ...codersdk.Template) string {
tableWriter := cliui.Table()
header := table.Row{
"Name", "Created At", "Last Updated", "Organization ID", "Provisioner",
"Active Version ID", "Used By", "Max TTL", "Min Autostart"}
tableWriter.AppendHeader(header)
tableWriter.SetColumnConfigs(cliui.FilterTableColumns(header, filterColumns))
tableWriter.SortBy([]table.SortBy{{
Name: "name",
}})
for _, template := range templates {
func displayTemplates(filterColumns []string, templates ...codersdk.Template) (string, error) {
rows := make([]templateTableRow, len(templates))
for i, template := range templates {
suffix := ""
if template.WorkspaceOwnerCount != 1 {
suffix = "s"
}
tableWriter.AppendRow(table.Row{
template.Name,
template.CreatedAt.Format("January 2, 2006"),
template.UpdatedAt.Format("January 2, 2006"),
template.OrganizationID.String(),
template.Provisioner,
template.ActiveVersionID.String(),
cliui.Styles.Fuchsia.Render(fmt.Sprintf("%d developer%s", template.WorkspaceOwnerCount, suffix)),
(time.Duration(template.MaxTTLMillis) * time.Millisecond).String(),
(time.Duration(template.MinAutostartIntervalMillis) * time.Millisecond).String(),
})
rows[i] = templateTableRow{
Name: template.Name,
CreatedAt: template.CreatedAt.Format("January 2, 2006"),
LastUpdated: template.UpdatedAt.Format("January 2, 2006"),
OrganizationID: template.OrganizationID,
Provisioner: template.Provisioner,
ActiveVersionID: template.ActiveVersionID,
UsedBy: cliui.Styles.Fuchsia.Render(fmt.Sprintf("%d developer%s", template.WorkspaceOwnerCount, suffix)),
MaxTTL: (time.Duration(template.MaxTTLMillis) * time.Millisecond),
MinAutostartInterval: (time.Duration(template.MinAutostartIntervalMillis) * time.Millisecond),
}
return tableWriter.Render()
}
return cliui.DisplayTable(rows, "name", filterColumns)
}

View File

@ -3,9 +3,9 @@ package cli
import (
"fmt"
"strings"
"time"
"github.com/google/uuid"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
"golang.org/x/xerrors"
@ -58,31 +58,44 @@ func templateVersionsList() *cobra.Command {
if err != nil {
return xerrors.Errorf("get template versions by template: %w", err)
}
_, err = fmt.Fprintln(cmd.OutOrStdout(), displayTemplateVersions(template.ActiveVersionID, versions...))
out, err := displayTemplateVersions(template.ActiveVersionID, versions...)
if err != nil {
return xerrors.Errorf("render table: %w", err)
}
_, err = fmt.Fprintln(cmd.OutOrStdout(), out)
return err
},
}
}
type templateVersionRow struct {
Name string `table:"name"`
CreatedAt time.Time `table:"created at"`
CreatedBy string `table:"created by"`
Status string `table:"status"`
Active string `table:"active"`
}
// displayTemplateVersions will return a table displaying existing
// template versions for the specified template.
func displayTemplateVersions(activeVersionID uuid.UUID, templateVersions ...codersdk.TemplateVersion) string {
tableWriter := cliui.Table()
header := table.Row{
"Name", "Created At", "Created By", "Status", ""}
tableWriter.AppendHeader(header)
for _, templateVersion := range templateVersions {
func displayTemplateVersions(activeVersionID uuid.UUID, templateVersions ...codersdk.TemplateVersion) (string, error) {
rows := make([]templateVersionRow, len(templateVersions))
for i, templateVersion := range templateVersions {
var activeStatus = ""
if templateVersion.ID == activeVersionID {
activeStatus = cliui.Styles.Code.Render(cliui.Styles.Keyword.Render("Active"))
}
tableWriter.AppendRow(table.Row{
templateVersion.Name,
templateVersion.CreatedAt.Format("03:04:05 PM MST on Jan 2, 2006"),
templateVersion.CreatedByName,
strings.Title(string(templateVersion.Job.Status)),
activeStatus,
})
rows[i] = templateVersionRow{
Name: templateVersion.Name,
CreatedAt: templateVersion.CreatedAt,
CreatedBy: templateVersion.CreatedByName,
Status: strings.Title(string(templateVersion.Job.Status)),
Active: activeStatus,
}
return tableWriter.Render()
}
return cliui.DisplayTable(rows, "name", nil)
}

View File

@ -111,13 +111,13 @@ func userSingle() *cobra.Command {
}
func displayUser(ctx context.Context, stderr io.Writer, client *codersdk.Client, user codersdk.User) string {
tableWriter := cliui.Table()
tw := cliui.Table()
addRow := func(name string, value interface{}) {
key := ""
if name != "" {
key = name + ":"
}
tableWriter.AppendRow(table.Row{
tw.AppendRow(table.Row{
key, value,
})
}
@ -170,5 +170,5 @@ func displayUser(ctx context.Context, stderr io.Writer, client *codersdk.Client,
addRow("Organizations", "(none)")
}
return tableWriter.Render()
return tw.Render()
}

View File

@ -50,14 +50,14 @@ type ComputedParameter struct {
// Parameter represents a set value for the scope.
type Parameter struct {
ID uuid.UUID `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Scope ParameterScope `json:"scope"`
ScopeID uuid.UUID `json:"scope_id"`
Name string `json:"name"`
SourceScheme ParameterSourceScheme `json:"source_scheme"`
DestinationScheme ParameterDestinationScheme `json:"destination_scheme"`
ID uuid.UUID `json:"id" table:"id"`
Scope ParameterScope `json:"scope" table:"scope"`
ScopeID uuid.UUID `json:"scope_id" table:"scope id"`
Name string `json:"name" table:"name"`
SourceScheme ParameterSourceScheme `json:"source_scheme" table:"source scheme"`
DestinationScheme ParameterDestinationScheme `json:"destination_scheme" table:"destination scheme"`
CreatedAt time.Time `json:"created_at" table:"created at"`
UpdatedAt time.Time `json:"updated_at" table:"updated at"`
}
type ParameterSchema struct {

View File

@ -205,13 +205,13 @@ export interface Pagination {
// From codersdk/parameters.go
export interface Parameter {
readonly id: string
readonly created_at: string
readonly updated_at: string
readonly scope: ParameterScope
readonly scope_id: string
readonly name: string
readonly source_scheme: ParameterSourceScheme
readonly destination_scheme: ParameterDestinationScheme
readonly created_at: string
readonly updated_at: string
}
// From codersdk/parameters.go