-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
renderer.go
181 lines (155 loc) · 4.34 KB
/
renderer.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
package cmd
import (
"fmt"
"io"
"strings"
"github.com/olekukonko/tablewriter"
"github.com/smartcontractkit/chainlink/v2/core/utils"
webpresenters "github.com/smartcontractkit/chainlink/v2/core/web/presenters"
)
// Renderer implements the Render method.
type Renderer interface {
Render(interface{}, ...string) error
}
// RendererJSON is used to render JSON data.
type RendererJSON struct {
io.Writer
}
// Render writes the given input as a JSON string.
func (rj RendererJSON) Render(v interface{}, _ ...string) error {
b, err := utils.FormatJSON(v)
if err != nil {
return err
}
// Append a new line
b = append(b, []byte("\n")...)
if _, err = rj.Write(b); err != nil {
return err
}
return nil
}
// RendererTable is used for data to be rendered as a table.
type RendererTable struct {
io.Writer
}
type TableRenderer interface {
RenderTable(rt RendererTable) error
}
// Render returns a formatted table of text for a given Job or presenter
// and relevant information.
func (rt RendererTable) Render(v interface{}, headers ...string) error {
for _, h := range headers {
fmt.Println(h)
}
switch typed := v.(type) {
case *webpresenters.ExternalInitiatorAuthentication:
return rt.renderExternalInitiatorAuthentication(*typed)
case *webpresenters.PipelineRunResource:
return rt.renderPipelineRun(*typed)
case *webpresenters.ServiceLogConfigResource:
return rt.renderLogPkgConfig(*typed)
case *[]VRFKeyPresenter:
return rt.renderVRFKeys(*typed)
case TableRenderer:
return typed.RenderTable(rt)
default:
return fmt.Errorf("unable to render object of type %T: %v", typed, typed)
}
}
func (rt RendererTable) renderLogPkgConfig(serviceLevelLog webpresenters.ServiceLogConfigResource) error {
table := rt.newTable([]string{"ID", "Service", "LogLevel"})
for i, svcName := range serviceLevelLog.ServiceName {
table.Append([]string{
serviceLevelLog.ID,
svcName,
serviceLevelLog.LogLevel[i],
})
}
render("ServiceLogConfig", table)
return nil
}
func (rt RendererTable) renderVRFKeys(keys []VRFKeyPresenter) error {
var rows [][]string
for _, key := range keys {
rows = append(rows, []string{
key.Compressed,
key.Uncompressed,
key.Hash,
})
}
renderList([]string{"Compressed", "Uncompressed", "Hash"}, rows, rt.Writer)
return nil
}
func render(name string, table *tablewriter.Table) {
table.SetRowLine(true)
table.SetColumnSeparator("║")
table.SetRowSeparator("═")
table.SetCenterSeparator("╬")
fmt.Println("╔ " + name)
table.Render()
}
func renderList(fields []string, items [][]string, writer io.Writer) {
var maxLabelLength int
for _, field := range fields {
if len(field) > maxLabelLength {
maxLabelLength = len(field)
}
}
var itemsRendered []string
var maxLineLength int
for _, row := range items {
var lines []string
for i, field := range fields {
diff := maxLabelLength - len(field)
spaces := strings.Repeat(" ", diff)
line := fmt.Sprintf("%v: %v%v", field, spaces, row[i])
for _, l := range strings.Split(line, "\n") {
if len(l) > maxLineLength {
maxLineLength = len(l)
}
}
lines = append(lines, line)
}
itemsRendered = append(itemsRendered, strings.Join(lines, "\n"))
}
divider := "\n" + strings.Repeat("-", maxLineLength) + "\n"
listRendered := divider + strings.Join(itemsRendered, divider) + divider
_, err := writer.Write([]byte(listRendered))
if err != nil {
// Handles errcheck
return
}
}
func (rt RendererTable) renderExternalInitiatorAuthentication(eia webpresenters.ExternalInitiatorAuthentication) error {
table := rt.newTable([]string{"Name", "URL", "AccessKey", "Secret", "OutgoingToken", "OutgoingSecret"})
table.Append([]string{
eia.Name,
eia.URL.String(),
eia.AccessKey,
eia.Secret,
eia.OutgoingToken,
eia.OutgoingSecret,
})
render("External Initiator Credentials:", table)
return nil
}
func (rt RendererTable) newTable(headers []string) *tablewriter.Table {
table := tablewriter.NewWriter(rt)
table.SetHeader(headers)
return table
}
func (rt RendererTable) renderPipelineRun(run webpresenters.PipelineRunResource) error {
table := rt.newTable([]string{"ID", "Created At", "Finished At"})
var finishedAt string
if !run.FinishedAt.IsZero() {
finishedAt = run.FinishedAt.ValueOrZero().String()
}
row := []string{
run.GetID(),
run.CreatedAt.String(),
finishedAt,
}
table.Append(row)
render("Pipeline Run", table)
return nil
}