forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.go
More file actions
204 lines (179 loc) · 4.48 KB
/
template.go
File metadata and controls
204 lines (179 loc) · 4.48 KB
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
package export
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math"
"strconv"
"strings"
"text/template"
"time"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/cli/cli/v2/pkg/text"
"github.com/cli/cli/v2/utils"
"github.com/mgutz/ansi"
)
type Template struct {
io *iostreams.IOStreams
tablePrinter utils.TablePrinter
template *template.Template
templateStr string
}
func NewTemplate(io *iostreams.IOStreams, template string) Template {
return Template{
io: io,
templateStr: template,
}
}
func (t *Template) parseTemplate(tpl string) (*template.Template, error) {
now := time.Now()
templateFuncs := map[string]interface{}{
"color": t.color,
"autocolor": t.color,
"timefmt": func(format, input string) (string, error) {
t, err := time.Parse(time.RFC3339, input)
if err != nil {
return "", err
}
return t.Format(format), nil
},
"timeago": func(input string) (string, error) {
t, err := time.Parse(time.RFC3339, input)
if err != nil {
return "", err
}
return timeAgo(now.Sub(t)), nil
},
"pluck": templatePluck,
"join": templateJoin,
"tablerow": t.tableRow,
"tablerender": t.tableRender,
"truncate": text.Truncate,
}
if !t.io.ColorEnabled() {
templateFuncs["autocolor"] = func(colorName string, input interface{}) (string, error) {
return jsonScalarToString(input)
}
}
return template.New("").Funcs(templateFuncs).Parse(tpl)
}
func (t *Template) Execute(input io.Reader) error {
w := t.io.Out
if t.template == nil {
template, err := t.parseTemplate(t.templateStr)
if err != nil {
return err
}
t.template = template
}
jsonData, err := ioutil.ReadAll(input)
if err != nil {
return err
}
var data interface{}
if err := json.Unmarshal(jsonData, &data); err != nil {
return err
}
return t.template.Execute(w, data)
}
func ExecuteTemplate(io *iostreams.IOStreams, input io.Reader, template string) error {
t := NewTemplate(io, template)
if err := t.Execute(input); err != nil {
return err
}
return t.End()
}
func jsonScalarToString(input interface{}) (string, error) {
switch tt := input.(type) {
case string:
return tt, nil
case float64:
if math.Trunc(tt) == tt {
return strconv.FormatFloat(tt, 'f', 0, 64), nil
} else {
return strconv.FormatFloat(tt, 'f', 2, 64), nil
}
case nil:
return "", nil
case bool:
return fmt.Sprintf("%v", tt), nil
default:
return "", fmt.Errorf("cannot convert type to string: %v", tt)
}
}
func (t *Template) color(colorName string, input interface{}) (string, error) {
text, err := jsonScalarToString(input)
if err != nil {
return "", err
}
return ansi.Color(text, colorName), nil
}
func templatePluck(field string, input []interface{}) []interface{} {
var results []interface{}
for _, item := range input {
obj := item.(map[string]interface{})
results = append(results, obj[field])
}
return results
}
func templateJoin(sep string, input []interface{}) (string, error) {
var results []string
for _, item := range input {
text, err := jsonScalarToString(item)
if err != nil {
return "", err
}
results = append(results, text)
}
return strings.Join(results, sep), nil
}
func (t *Template) tableRow(fields ...interface{}) (string, error) {
if t.tablePrinter == nil {
t.tablePrinter = utils.NewTablePrinterWithOptions(t.io, utils.TablePrinterOptions{IsTTY: true})
}
for _, e := range fields {
s, err := jsonScalarToString(e)
if err != nil {
return "", fmt.Errorf("failed to write table row: %v", err)
}
t.tablePrinter.AddField(s, text.TruncateColumn, nil)
}
t.tablePrinter.EndRow()
return "", nil
}
func (t *Template) tableRender() (string, error) {
if t.tablePrinter != nil {
err := t.tablePrinter.Render()
t.tablePrinter = nil
if err != nil {
return "", fmt.Errorf("failed to render table: %v", err)
}
}
return "", nil
}
func (t *Template) End() error {
// Finalize any template actions.
if _, err := t.tableRender(); err != nil {
return err
}
return nil
}
func timeAgo(ago time.Duration) string {
if ago < time.Minute {
return "just now"
}
if ago < time.Hour {
return utils.Pluralize(int(ago.Minutes()), "minute") + " ago"
}
if ago < 24*time.Hour {
return utils.Pluralize(int(ago.Hours()), "hour") + " ago"
}
if ago < 30*24*time.Hour {
return utils.Pluralize(int(ago.Hours())/24, "day") + " ago"
}
if ago < 365*24*time.Hour {
return utils.Pluralize(int(ago.Hours())/24/30, "month") + " ago"
}
return utils.Pluralize(int(ago.Hours()/24/365), "year") + " ago"
}