-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathplaintext_fns.go
More file actions
81 lines (70 loc) · 2.25 KB
/
plaintext_fns.go
File metadata and controls
81 lines (70 loc) · 2.25 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
package output
import (
"fmt"
"sort"
"strings"
)
// ConfigPlaintextOutputFn converts the resource to plain text specifically for data from the
// config file.
var ConfigPlaintextOutputFn = func(r resource) string {
keys := make([]string, 0)
for k := range r {
keys = append(keys, k)
}
sort.Strings(keys)
lst := make([]string, 0)
for _, k := range keys {
lst = append(lst, fmt.Sprintf("%s: %v", k, r[k]))
}
return strings.Join(lst, "\n")
}
// ErrorPlaintextOutputFn converts the resource to plain text specifically for data from the
// error file.
// An error response could have a code and message or just a message. It's also possible that
// there isn't either property.
var ErrorPlaintextOutputFn = func(r resource) string {
var parts []string
switch {
case r["code"] == nil && (r["message"] == "" || r["message"] == nil):
parts = append(parts, "unknown error occurred")
case r["code"] == nil:
parts = append(parts, fmt.Sprint(r["message"]))
case r["message"] == "":
parts = append(parts, fmt.Sprintf("an error occurred (code: %v)", r["code"]))
default:
parts = append(parts, fmt.Sprintf("%v (code: %v)", r["message"], r["code"]))
}
if suggestion, ok := r["suggestion"]; ok && suggestion != nil && suggestion != "" {
parts = append(parts, fmt.Sprintf("\nSuggestion: %s", suggestion))
}
return strings.Join(parts, "")
}
// MultiplePlaintextOutputFn converts the resource to plain text.
var MultiplePlaintextOutputFn = func(r resource) string {
return fmt.Sprintf("* %s", SingularPlaintextOutputFn(r))
}
// SingularPlaintextOutputFn converts the resource to plain text based on its name and key.
var SingularPlaintextOutputFn = func(r resource) string {
email := r["email"]
id := r["_id"]
key := r["key"]
name := r["name"]
switch {
case name != nil && key != nil:
return fmt.Sprintf("%s (%s)", fmt.Sprint(name), fmt.Sprint(key))
case email != nil && id != nil:
return fmt.Sprintf("%s (%s)", fmt.Sprint(email), fmt.Sprint(id))
case name != nil && id != nil:
return fmt.Sprintf("%s (%s)", fmt.Sprint(name), fmt.Sprint(id))
case key != nil:
return fmt.Sprint(key)
case email != nil:
return fmt.Sprint(email)
case id != nil:
return fmt.Sprint(id)
case name != nil:
return fmt.Sprint(name)
default:
return "cannot read resource"
}
}