-
Notifications
You must be signed in to change notification settings - Fork 289
/
format.go
367 lines (334 loc) · 9.44 KB
/
format.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package base
import (
"encoding/json"
"fmt"
"os"
"sort"
"strings"
"unicode"
"unicode/utf8"
"github.com/hashicorp/boundary/api"
"github.com/hashicorp/boundary/api/plugins"
"github.com/hashicorp/boundary/api/scopes"
"github.com/mitchellh/cli"
"github.com/mitchellh/go-wordwrap"
"github.com/pkg/errors"
)
// This is adapted from the code in the strings package for TrimSpace
var asciiSpace = [256]uint8{'\t': 1, '\n': 1, '\v': 1, '\f': 1, '\r': 1, ' ': 1}
func ScopeInfoForOutput(scp *scopes.ScopeInfo, maxLength int) string {
if scp == nil {
return " <not included in response>"
}
vals := map[string]interface{}{
"ID": scp.Id,
"Type": scp.Type,
"Name": scp.Name,
}
if scp.ParentScopeId != "" {
vals["Parent Scope ID"] = scp.ParentScopeId
}
return WrapMap(4, maxLength, vals)
}
func PluginInfoForOutput(plg *plugins.PluginInfo, maxLength int) string {
if plg == nil {
return " <not included in response>"
}
vals := map[string]interface{}{
"ID": plg.Id,
"Name": plg.Name,
}
return WrapMap(4, maxLength, vals)
}
func MaxAttributesLength(nonAttributesMap, attributesMap map[string]interface{}, keySubstMap map[string]string) int {
// We always print a scope ID and in some cases this particular key ends up
// being the longest key, so start with it as a baseline. It's always
// indented by 2 in addition to the normal offset so take that into account.
maxLength := len("Parent Scope ID") + 2
for k := range nonAttributesMap {
if len(k) > maxLength {
maxLength = len(k)
}
}
if len(attributesMap) > 0 {
for k, v := range attributesMap {
if keySubstMap != nil {
if keySubstMap[k] != "" {
attributesMap[keySubstMap[k]] = v
delete(attributesMap, k)
}
}
}
for k := range attributesMap {
if len(k) > maxLength {
maxLength = len(k)
}
}
}
return maxLength
}
func trimSpaceRight(in string) string {
for stop := len(in); stop > 0; stop-- {
c := in[stop-1]
if c >= utf8.RuneSelf {
return strings.TrimFunc(in[:stop], unicode.IsSpace)
}
if asciiSpace[c] == 0 {
return in[0:stop]
}
}
return ""
}
func WrapForHelpText(lines []string) string {
var ret []string
for _, line := range lines {
line = trimSpaceRight(line)
trimmed := strings.TrimSpace(line)
diff := uint(len(line) - len(trimmed))
wrapped := wordwrap.WrapString(trimmed, TermWidth-diff)
splitWrapped := strings.Split(wrapped, "\n")
for i := range splitWrapped {
splitWrapped[i] = fmt.Sprintf("%s%s", strings.Repeat(" ", int(diff)), strings.TrimSpace(splitWrapped[i]))
}
ret = append(ret, strings.Join(splitWrapped, "\n"))
}
return strings.Join(ret, "\n")
}
func WrapSlice(prefixSpaces int, input []string) string {
var ret []string
for _, v := range input {
ret = append(ret, fmt.Sprintf("%s%s",
strings.Repeat(" ", prefixSpaces),
v,
))
}
return strings.Join(ret, "\n")
}
func WrapMap(prefixSpaces, maxLengthOverride int, input map[string]interface{}) string {
maxKeyLength := maxLengthOverride
if maxKeyLength == 0 {
for k := range input {
if len(k) > maxKeyLength {
maxKeyLength = len(k)
}
}
}
var sortedKeys []string
for k := range input {
sortedKeys = append(sortedKeys, k)
}
sort.Strings(sortedKeys)
var ret []string
for _, k := range sortedKeys {
v := input[k]
spaces := maxKeyLength - len(k)
if spaces < 0 {
spaces = 0
}
if sv, ok := v.([]string); ok {
nv := make([]string, 0, len(sv))
for _, si := range sv {
nv = append(nv, fmt.Sprintf("%q", si))
}
v = nv
}
vOut := fmt.Sprintf("%v", v)
switch v.(type) {
case map[string]interface{}:
buf, err := json.MarshalIndent(v, strings.Repeat(" ", prefixSpaces), " ")
if err != nil {
vOut = "[Unable to Print]"
break
}
bStrings := strings.Split(string(buf), "\n")
if len(bStrings) > 0 {
// Indent doesn't apply to the first line 🙄
bStrings[0] = fmt.Sprintf("\n%s%s", strings.Repeat(" ", prefixSpaces), bStrings[0])
}
vOut = strings.Join(bStrings, "\n")
}
ret = append(ret, fmt.Sprintf("%s%s%s%s",
strings.Repeat(" ", prefixSpaces),
fmt.Sprintf("%s: ", k),
strings.Repeat(" ", spaces),
vOut,
))
}
return strings.Join(ret, "\n")
}
// PrintApiError prints the given API error, optionally with context
// information, to the UI in the appropriate format. WithAttributeFieldPrefix is
// used, all other options are ignored.
func (c *Command) PrintApiError(in *api.Error, contextStr string, opt ...Option) {
opts := getOpts(opt...)
switch Format(c.UI) {
case "json":
output := struct {
Context string `json:"context,omitempty"`
Status int `json:"status"`
ApiError json.RawMessage `json:"api_error"`
}{
Context: contextStr,
Status: in.Response().StatusCode(),
ApiError: in.Response().Body.Bytes(),
}
b, _ := JsonFormatter{}.Format(output)
c.UI.Error(string(b))
default:
nonAttributeMap := map[string]interface{}{
"Status": in.Response().StatusCode(),
"Kind": in.Kind,
"Message": in.Message,
}
if contextStr != "" {
nonAttributeMap["context"] = contextStr
}
if in.Op != "" {
nonAttributeMap["Operation"] = in.Op
}
maxLength := MaxAttributesLength(nonAttributeMap, nil, nil)
var output []string
if contextStr != "" {
output = append(output, contextStr)
}
output = append(output,
"",
"Error information:",
WrapMap(2, maxLength+2, nonAttributeMap),
)
if in.Details != nil {
if len(in.Details.WrappedErrors) > 0 {
output = append(output,
"",
" Wrapped Errors:",
)
for _, we := range in.Details.WrappedErrors {
output = append(output,
fmt.Sprintf(" Message: %s", we.Message),
fmt.Sprintf(" Operation: %s", we.Op),
)
}
}
if len(in.Details.RequestFields) > 0 {
output = append(output,
"",
" Field-specific Errors:",
)
for _, field := range in.Details.RequestFields {
if field.Name == "update_mask" {
// TODO: Report useful error messages related to "update_mask".
continue
}
var fNameParts []string
if opts.withAttributeFieldPrefix != "" && strings.HasPrefix(field.Name, "attributes.") {
fNameParts = append(fNameParts, opts.withAttributeFieldPrefix)
}
fNameParts = append(fNameParts, strings.ReplaceAll(strings.TrimPrefix(field.Name, "attributes."), "_", "-"))
fName := strings.Join(fNameParts, "-")
output = append(output,
fmt.Sprintf(" Name: -%s", fName),
fmt.Sprintf(" Error: %s", field.Description),
)
}
}
}
c.UI.Error(WrapForHelpText(output))
}
}
// PrintCliError prints the given CLI error to the UI in the appropriate format
func (c *Command) PrintCliError(err error) {
switch Format(c.UI) {
case "table":
c.UI.Error(err.Error())
case "json":
output := struct {
Error string `json:"error"`
}{
Error: err.Error(),
}
b, _ := JsonFormatter{}.Format(output)
c.UI.Error(string(b))
}
}
// PrintJsonItem prints the given item to the UI in JSON format
func (c *Command) PrintJsonItem(result api.GenericResult, opt ...Option) bool {
resp := result.GetResponse()
if resp == nil {
c.PrintCliError(errors.New("Error formatting as JSON: no response given to item formatter"))
return false
}
if r := resp.HttpResponse(); r != nil {
opt = append(opt, WithStatusCode(r.StatusCode))
}
return c.PrintJson(resp.Body.Bytes(), opt...)
}
// PrintJson prints the given raw JSON in our common format
func (c *Command) PrintJson(input json.RawMessage, opt ...Option) bool {
opts := getOpts(opt...)
output := struct {
StatusCode int `json:"status_code,omitempty"`
Item json.RawMessage `json:"item,omitempty"`
}{
StatusCode: opts.withStatusCode,
Item: input,
}
b, err := JsonFormatter{}.Format(output)
if err != nil {
c.PrintCliError(fmt.Errorf("Error formatting as JSON: %w", err))
return false
}
c.UI.Output(string(b))
return true
}
// PrintJsonItems prints the given items to the UI in JSON format
func (c *Command) PrintJsonItems(result api.GenericListResult) bool {
resp := result.GetResponse()
if resp == nil {
c.PrintCliError(errors.New("Error formatting as JSON: no response given to items formatter"))
return false
}
// First we need to grab the items out. The reason is that if we simply
// embed the raw message as with PrintJsonItem above, it will have {"items":
// {"items": []}}. However, we decode into a RawMessage which makes it much
// more efficient on both the decoding and encoding side.
type inMsg struct {
Items json.RawMessage `json:"items"`
}
var input inMsg
if resp.Body.Bytes() != nil {
if err := json.Unmarshal(resp.Body.Bytes(), &input); err != nil {
c.PrintCliError(fmt.Errorf("Error unmarshaling response body at format time: %w", err))
return false
}
}
output := struct {
StatusCode int `json:"status_code"`
Items json.RawMessage `json:"items"`
}{
StatusCode: resp.HttpResponse().StatusCode,
Items: input.Items,
}
b, err := JsonFormatter{}.Format(output)
if err != nil {
c.PrintCliError(fmt.Errorf("Error formatting as JSON: %w", err))
return false
}
c.UI.Output(string(b))
return true
}
// An output formatter for json output of an object
type JsonFormatter struct{}
func (j JsonFormatter) Format(data interface{}) ([]byte, error) {
return json.Marshal(data)
}
func Format(ui cli.Ui) string {
switch t := ui.(type) {
case *BoundaryUI:
return t.Format
}
format := os.Getenv(EnvBoundaryCLIFormat)
if format == "" {
format = "table"
}
return format
}