Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
reuse JSONToTable
Browse files Browse the repository at this point in the history
Signed-off-by: zychen5186 <[email protected]>
  • Loading branch information
zychen5186 committed Apr 23, 2024
1 parent 44e2676 commit b2dee42
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 68 deletions.
1 change: 1 addition & 0 deletions cmd/get/matchable_task_resource_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion cmd/register/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
65 changes: 4 additions & 61 deletions pkg/bubbletea/bubbletea_pagination_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions pkg/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/url"
"os"
"sort"
Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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
}

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

Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"os"
"testing"
"time"

Expand Down Expand Up @@ -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},
}))
Expand Down

0 comments on commit b2dee42

Please sign in to comment.