From b2dee428f4241c5d257689121d71b7d0706b6a5e Mon Sep 17 00:00:00 2001 From: zychen5186 Date: Tue, 23 Apr 2024 15:10:21 -0700 Subject: [PATCH] reuse JSONToTable Signed-off-by: zychen5186 --- cmd/get/matchable_task_resource_attribute.go | 1 + cmd/register/files.go | 2 +- pkg/bubbletea/bubbletea_pagination_util.go | 65 ++------------------ pkg/printer/printer.go | 10 +-- pkg/printer/printer_test.go | 3 +- 5 files changed, 13 insertions(+), 68 deletions(-) diff --git a/cmd/get/matchable_task_resource_attribute.go b/cmd/get/matchable_task_resource_attribute.go index cfc5f4c6..c27a0b66 100644 --- a/cmd/get/matchable_task_resource_attribute.go +++ b/cmd/get/matchable_task_resource_attribute.go @@ -2,6 +2,7 @@ package get import ( "context" + "github.com/flyteorg/flyte/flyteidl/gen/pb-go/flyteidl/admin" "github.com/flyteorg/flytectl/cmd/config" sconfig "github.com/flyteorg/flytectl/cmd/config/subcommand" diff --git a/cmd/register/files.go b/cmd/register/files.go index f9b70696..bc204990 100644 --- a/cmd/register/files.go +++ b/cmd/register/files.go @@ -162,7 +162,7 @@ func Register(ctx context.Context, args []string, cfg *config.Config, cmdCtx cmd payload, _ := json.Marshal(registerResults) registerPrinter := printer.Printer{} - _ = registerPrinter.JSONToTable(payload, projectColumns) + _ = registerPrinter.JSONToTable(os.Stdout, payload, projectColumns) if tmpDir != "" { if _err := os.RemoveAll(tmpDir); _err != nil { logger.Errorf(ctx, "unable to delete temp dir %v due to %v", tmpDir, _err) diff --git a/pkg/bubbletea/bubbletea_pagination_util.go b/pkg/bubbletea/bubbletea_pagination_util.go index a904caa7..50520860 100644 --- a/pkg/bubbletea/bubbletea_pagination_util.go +++ b/pkg/bubbletea/bubbletea_pagination_util.go @@ -8,11 +8,9 @@ import ( "github.com/flyteorg/flytectl/pkg/filters" "github.com/flyteorg/flytectl/pkg/printer" + "github.com/golang/protobuf/jsonpb" "github.com/golang/protobuf/proto" - "github.com/kataras/tablewriter" - "github.com/landoop/tableprinter" - "github.com/yalp/jsonpath" ) type DataCallback func(filter filters.Filters) []proto.Message @@ -49,37 +47,6 @@ func (p PrintableProto) MarshalJSON() ([]byte, error) { return buf.Bytes(), nil } -func extractRow(data interface{}, columns []printer.Column) []string { - if columns == nil || data == nil { - return nil - } - - tableData := make([]string, 0, len(columns)) - for _, c := range columns { - out, err := jsonpath.Read(data, c.JSONPath) - if err != nil || out == nil { - out = "" - } - s := fmt.Sprintf("%s", out) - if c.TruncateTo != nil { - t := *c.TruncateTo - if len(s) > t { - s = s[:t] - } - } - tableData = append(tableData, s) - } - return tableData -} - -func projectColumns(rows []interface{}, column []printer.Column) [][]string { - responses := make([][]string, 0, len(rows)) - for _, row := range rows { - responses = append(responses, extractRow(row, column)) - } - return responses -} - func printTable(m *pageModel, start int, end int) (string, error) { curShowMessage := m.items[start:end] printableMessages := make([]*PrintableProto, 0, len(curShowMessage)) @@ -92,34 +59,10 @@ func printTable(m *pageModel, start int, end int) (string, error) { return "", fmt.Errorf("failed to marshal proto messages") } - var rawRows []interface{} - if err := json.Unmarshal(jsonRows, &rawRows); err != nil { - return "", fmt.Errorf("failed to unmarshal into []interface{} from json") - } - if rawRows == nil { - return "", fmt.Errorf("expected one row or empty rows, received nil") - } - rows := projectColumns(rawRows, listHeader) - var buf strings.Builder - printer := tableprinter.New(&buf) - printer.AutoWrapText = false - printer.BorderLeft = true - printer.BorderRight = true - printer.BorderBottom = true - printer.BorderTop = true - printer.RowLine = true - printer.ColumnSeparator = "|" - printer.HeaderBgColor = tablewriter.BgHiWhiteColor - headers := make([]string, 0, len(listHeader)) - positions := make([]int, 0, len(listHeader)) - for _, c := range listHeader { - headers = append(headers, c.Header) - positions = append(positions, 30) - } - - if r := printer.Render(headers, rows, positions, true); r == -1 { - return "", fmt.Errorf("failed to render table") + p := printer.Printer{} + if err := p.JSONToTable(&buf, jsonRows, listHeader); err != nil { + return "", err } return buf.String(), nil diff --git a/pkg/printer/printer.go b/pkg/printer/printer.go index e3b406b9..f98b76bf 100644 --- a/pkg/printer/printer.go +++ b/pkg/printer/printer.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "io" "net/url" "os" "sort" @@ -112,7 +113,7 @@ func projectColumns(rows []interface{}, column []Column) [][]string { return responses } -func (p Printer) JSONToTable(jsonRows []byte, columns []Column) error { +func (p Printer) JSONToTable(w io.Writer, jsonRows []byte, columns []Column) error { var rawRows []interface{} if err := json.Unmarshal(jsonRows, &rawRows); err != nil { return errors.Wrapf("JSONUnmarshalFailure", err, "failed to unmarshal into []interface{} from json") @@ -122,7 +123,7 @@ func (p Printer) JSONToTable(jsonRows []byte, columns []Column) error { } rows := projectColumns(rawRows, columns) - printer := tableprinter.New(os.Stdout) + printer := tableprinter.New(w) // TODO make this configurable printer.AutoWrapText = false printer.BorderLeft = true @@ -141,7 +142,6 @@ func (p Printer) JSONToTable(jsonRows []byte, columns []Column) error { if r := printer.Render(headers, rows, positions, true); r == -1 { return fmt.Errorf("failed to render table") } - fmt.Printf("%d rows\n", len(rows)) return nil } @@ -155,7 +155,7 @@ func (p Printer) PrintInterface(format OutputFormat, columns []Column, v interfa case OutputFormatJSON, OutputFormatYAML: return printJSONYaml(format, v) default: // Print table - return p.JSONToTable(jsonRows, columns) + return p.JSONToTable(os.Stdout, jsonRows, columns) } } @@ -285,7 +285,7 @@ func (p Printer) Print(format OutputFormat, columns []Column, messages ...proto. if err != nil { return errors.Wrapf("ProtoToJSONFailure", err, "failed to marshal proto messages") } - return p.JSONToTable(rows, columns) + return p.JSONToTable(os.Stdout, rows, columns) } return nil } diff --git a/pkg/printer/printer_test.go b/pkg/printer/printer_test.go index 06d4c2c3..eb4960a8 100644 --- a/pkg/printer/printer_test.go +++ b/pkg/printer/printer_test.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "os" "testing" "time" @@ -53,7 +54,7 @@ func TestJSONToTable(t *testing.T) { b, err := json.Marshal(j) assert.NoError(t, err) p := Printer{} - assert.NoError(t, p.JSONToTable(b, []Column{ + assert.NoError(t, p.JSONToTable(os.Stdout, b, []Column{ {"A", "$.a", &trunc}, {"S", "$.s.y", nil}, }))