This repository has been archived by the owner on May 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
printer.go
311 lines (277 loc) · 8.22 KB
/
printer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package printer
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/url"
"os"
"sort"
"strings"
"github.com/flyteorg/flyte/flyteidl/gen/pb-go/flyteidl/admin"
"github.com/flyteorg/flyte/flyteidl/gen/pb-go/flyteidl/core"
"github.com/flyteorg/flyte/flytestdlib/errors"
"github.com/flyteorg/flytectl/pkg/visualize"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/kataras/tablewriter"
"github.com/landoop/tableprinter"
"github.com/pkg/browser"
"github.com/yalp/jsonpath"
"sigs.k8s.io/yaml"
)
//go:generate enumer --type=OutputFormat -json -yaml -trimprefix=OutputFormat
type OutputFormat uint8
const (
OutputFormatTABLE OutputFormat = iota
OutputFormatJSON
OutputFormatYAML
OutputFormatDOT
OutputFormatDOTURL
)
// Set implements PFlag's Value interface to attempt to set the value of the flag from string.
func (i *OutputFormat) Set(val string) error {
policy, err := OutputFormatString(val)
if err != nil {
return err
}
*i = policy
return nil
}
// Type implements PFlag's Value interface to return type name.
func (i OutputFormat) Type() string {
return "OutputFormat"
}
const GraphVisualizationServiceURL = "http://graph.flyte.org/#"
func OutputFormats() []string {
var v []string
for _, o := range OutputFormatValues() {
v = append(v, o.String())
}
return v
}
type Column struct {
Header string
JSONPath string
// Optional Truncation directive to limit content. This will simply truncate the string output.
TruncateTo *int
}
type Printer struct{}
const (
empty = ""
tab = "\t"
DefaultFormattedDescriptionsKey = "_formatted_descriptions"
defaultLineWidth = 25
)
// Projects the columns in one row of data from the given JSON using the []Column map
func extractRow(data interface{}, columns []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
}
// Projects the columns from the given list of JSON elements using the []Column map
// Potential performance problem, as it returns all the rows in memory.
// We could use the render row, but that may lead to misalignment.
// TODO figure out a more optimal way
func projectColumns(rows []interface{}, column []Column) [][]string {
responses := make([][]string, 0, len(rows))
for _, row := range rows {
responses = append(responses, extractRow(row, column))
}
return responses
}
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")
}
if rawRows == nil {
return errors.Errorf("JSONUnmarshalNil", "expected one row or empty rows, received nil")
}
rows := projectColumns(rawRows, columns)
printer := tableprinter.New(w)
// TODO make this configurable
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(columns))
positions := make([]int, 0, len(columns))
for _, c := range columns {
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")
}
if w == os.Stdout {
fmt.Printf("%d rows\n", len(rows))
}
return nil
}
func (p Printer) PrintInterface(format OutputFormat, columns []Column, v interface{}) error {
jsonRows, err := json.Marshal(v)
if err != nil {
return err
}
// Factory Method for all printer
switch format {
case OutputFormatJSON, OutputFormatYAML:
return printJSONYaml(format, v)
default: // Print table
return p.JSONToTable(os.Stdout, jsonRows, columns)
}
}
// printJSONYaml internal function for printing
func printJSONYaml(format OutputFormat, v interface{}) error {
if format != OutputFormatJSON && format != OutputFormatYAML {
return fmt.Errorf("this function should be called only for json/yaml printing")
}
buf := new(bytes.Buffer)
encoder := json.NewEncoder(buf)
encoder.SetIndent(empty, tab)
err := encoder.Encode(v)
if err != nil {
return err
}
if format == OutputFormatJSON {
fmt.Println(buf.String())
} else {
v, err := yaml.JSONToYAML(buf.Bytes())
if err != nil {
return err
}
fmt.Println(string(v))
}
return nil
}
func FormatVariableDescriptions(variableMap map[string]*core.Variable) {
keys := make([]string, 0, len(variableMap))
// sort the keys for testing and consistency with other output formats
for k := range variableMap {
keys = append(keys, k)
}
sort.Strings(keys)
var descriptions []string
for _, k := range keys {
v := variableMap[k]
// a: a isn't very helpful
if k != v.Description {
descriptions = append(descriptions, getTruncatedLine(fmt.Sprintf("%s: %s", k, v.Description)))
} else {
descriptions = append(descriptions, getTruncatedLine(k))
}
}
variableMap[DefaultFormattedDescriptionsKey] = &core.Variable{Description: strings.Join(descriptions, "\n")}
}
func FormatParameterDescriptions(parameterMap map[string]*core.Parameter) {
keys := make([]string, 0, len(parameterMap))
// sort the keys for testing and consistency with other output formats
for k := range parameterMap {
keys = append(keys, k)
}
sort.Strings(keys)
var descriptions []string
for _, k := range keys {
v := parameterMap[k]
if v.Var == nil {
continue
}
// a: a isn't very helpful
if k != v.Var.Description {
descriptions = append(descriptions, getTruncatedLine(fmt.Sprintf("%s: %s", k, v.Var.Description)))
} else {
descriptions = append(descriptions, getTruncatedLine(k))
}
}
parameterMap[DefaultFormattedDescriptionsKey] = &core.Parameter{Var: &core.Variable{Description: strings.Join(descriptions, "\n")}}
}
func getTruncatedLine(line string) string {
// TODO: maybe add width to function signature later
width := defaultLineWidth
if len(line) > width {
return line[:width-3] + "..."
}
return line
}
func (p Printer) Print(format OutputFormat, columns []Column, messages ...proto.Message) error {
printableMessages := make([]*PrintableProto, 0, len(messages))
for _, m := range messages {
printableMessages = append(printableMessages, &PrintableProto{Message: m})
}
// Factory Method for all printer
switch format {
case OutputFormatJSON, OutputFormatYAML: // Print protobuf to json
var v interface{}
if len(printableMessages) == 1 {
v = printableMessages[0]
} else {
v = printableMessages
}
return printJSONYaml(format, v)
case OutputFormatDOT, OutputFormatDOTURL:
var workflows []*admin.Workflow
for _, m := range messages {
if w, ok := m.(*admin.Workflow); ok {
workflows = append(workflows, w)
} else {
return fmt.Errorf("visualization is only supported on workflows")
}
}
if len(workflows) == 0 {
return fmt.Errorf("atleast one workflow required for visualization")
}
workflow := workflows[0]
graphStr, err := visualize.RenderWorkflow(workflow.Closure.CompiledWorkflow)
if err != nil {
return errors.Wrapf("VisualizationError", err, "failed to visualize workflow")
}
if format == OutputFormatDOTURL {
urlToOpen := GraphVisualizationServiceURL + url.PathEscape(graphStr)
fmt.Println("Opening the browser at " + urlToOpen)
return browser.OpenURL(urlToOpen)
}
fmt.Println(graphStr)
default: // Print table
rows, err := json.Marshal(printableMessages)
if err != nil {
return errors.Wrapf("ProtoToJSONFailure", err, "failed to marshal proto messages")
}
return p.JSONToTable(os.Stdout, rows, columns)
}
return nil
}
type PrintableProto struct {
proto.Message
}
var marshaller = jsonpb.Marshaler{
Indent: tab,
}
func (p PrintableProto) MarshalJSON() ([]byte, error) {
buf := new(bytes.Buffer)
err := marshaller.Marshal(buf, p.Message)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}