Skip to content

feat(cli): add json output to coder speedtest #13475

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

Merged
merged 13 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
WIP
  • Loading branch information
ethanndickson committed Jun 4, 2024
commit ff91d688a4f04dfeadcc036abcf4f915ed5f7a88
67 changes: 1 addition & 66 deletions cli/cliui/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ import (
"fmt"
"reflect"
"strings"
"time"

"golang.org/x/xerrors"

"github.com/jedib0t/go-pretty/v6/table"

"github.com/coder/serpent"
)

Expand Down Expand Up @@ -146,69 +143,7 @@ func (f *tableFormat) AttachOptions(opts *serpent.OptionSet) {

// Format implements OutputFormat.
func (f *tableFormat) Format(_ context.Context, data any) (string, error) {
v := reflect.Indirect(reflect.ValueOf(data))

headers := make(table.Row, len(f.allColumns))
for i, header := range f.allColumns {
headers[i] = header
}
// Setup the table formatter.
tw := Table()
tw.AppendHeader(headers)
tw.SetColumnConfigs(filterTableColumns(headers, f.columns))
if f.sort != "" {
tw.SortBy([]table.SortBy{{
Name: f.sort,
}})
}

// Write each struct to the table.
for i := 0; i < v.Len(); i++ {
cur := v.Index(i).Interface()
_, ok := cur.(TableSeparator)
if ok {
tw.AppendSeparator()
continue
}
// Format the row as a slice.
// ValueToTableMap does what `reflect.Indirect` does
rowMap, err := valueToTableMap(reflect.ValueOf(cur))
if err != nil {
return "", xerrors.Errorf("get table row map %v: %w", i, err)
}

rowSlice := make([]any, len(headers))
for i, h := range f.allColumns {
v, ok := rowMap[h]
if !ok {
v = nil
}

// Special type formatting.
switch val := v.(type) {
case time.Time:
v = val.Format(time.RFC3339)
case *time.Time:
if val != nil {
v = val.Format(time.RFC3339)
}
case *int64:
if val != nil {
v = *val
}
case fmt.Stringer:
if val != nil {
v = val.String()
}
}

rowSlice[i] = v
}

tw.AppendRow(table.Row(rowSlice))
}

return tw.Render(), nil
return displayTable(data, f.sort, f.columns, nil)
}

type jsonFormat struct{}
Expand Down
73 changes: 65 additions & 8 deletions cli/cliui/table.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package cliui

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

"github.com/fatih/structtag"
"github.com/jedib0t/go-pretty/v6/table"
Expand Down Expand Up @@ -127,16 +127,73 @@ func DisplayAsTable(out any, sort string, filterColumns []string) (string, error
return "", xerrors.Errorf("specified sort column %q not found in table headers, available columns are %q", sort, strings.Join(headersRaw, `", "`))
}
}
return displayTable(out, sort, headersRaw, filterColumns)
}

func displayTable(out any, sort string, headersRaw []string, filterColumns []string) (string, error) {
v := reflect.Indirect(reflect.ValueOf(out))

headers := make(table.Row, len(headersRaw))
for i, header := range headersRaw {
headers[i] = header
}
// Setup the table formatter.
tw := Table()
tw.AppendHeader(headers)
tw.SetColumnConfigs(filterTableColumns(headers, filterColumns))
if sort != "" {
tw.SortBy([]table.SortBy{{
Name: sort,
}})
}

// Write each struct to the table.
for i := 0; i < v.Len(); i++ {
cur := v.Index(i).Interface()
_, ok := cur.(TableSeparator)
if ok {
tw.AppendSeparator()
continue
}
// Format the row as a slice.
// ValueToTableMap does what `reflect.Indirect` does
rowMap, err := valueToTableMap(reflect.ValueOf(cur))
if err != nil {
return "", xerrors.Errorf("get table row map %v: %w", i, err)
}

rowSlice := make([]any, len(headers))
for i, h := range headersRaw {
v, ok := rowMap[h]
if !ok {
v = nil
}

// Special type formatting.
switch val := v.(type) {
case time.Time:
v = val.Format(time.RFC3339)
case *time.Time:
if val != nil {
v = val.Format(time.RFC3339)
}
case *int64:
if val != nil {
v = *val
}
case fmt.Stringer:
if val != nil {
v = val.String()
}
}

rowSlice[i] = v
}

// TODO(ethan): remove type import?
tf := &tableFormat{
allColumns: headersRaw,
defaultColumns: headersRaw,
columns: filterColumns,
sort: sort,
tw.AppendRow(table.Row(rowSlice))
}

return tf.Format(context.Background(), out)
return tw.Render(), nil
}

// parseTableStructTag returns the name of the field according to the `table`
Expand Down