-
Notifications
You must be signed in to change notification settings - Fork 55
/
logs.go
185 lines (147 loc) · 3.54 KB
/
logs.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
package display
import (
"fmt"
"strings"
"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/auth0"
"github.com/auth0/go-auth0/management"
"gopkg.in/yaml.v2"
)
const (
notApplicable = "N/A"
logCategorySuccess logCategory = iota
logCategoryWarning
logCategoryFailure
logCategoryUnknown
)
type logCategory int
var _ View = &logView{}
type logView struct {
silent bool
*management.Log
raw interface{}
}
func (v *logView) AsTableHeader() []string {
return []string{"Type", "Description", "Date", "Connection", "Client"}
}
func (v *logView) getConnection() string {
if v.Details["prompts"] == nil {
return notApplicable
}
prompt, ok := v.Details["prompts"].([]interface{})
if ok && len(prompt) > 0 {
dict, ok := prompt[0].(map[string]interface{})
if ok {
v, ok := dict["connection"].(string)
if ok {
return v
}
return notApplicable
}
return notApplicable
}
return notApplicable
}
func (v *logView) AsTableRow() []string {
typ, desc := v.typeDesc()
clientName := v.GetClientName()
if clientName == "" {
clientName = ansi.Faint(notApplicable)
}
conn := v.getConnection()
if conn == notApplicable {
conn = ansi.Faint(truncate(conn, 20))
} else {
conn = truncate(conn, 20)
}
return []string{
typ,
truncate(desc, 54),
truncate(v.GetDate().Format("Jan 02 15:04:05.000"), 20),
conn,
clientName,
}
}
func (v *logView) Object() interface{} {
return v.raw
}
func (v *logView) Extras() []string {
if v.silent {
return nil
}
// NOTE(cyx): For now we only want to return full log information when
// it's an error.
if v.category() != logCategoryFailure {
return nil
}
raw, _ := yaml.Marshal(v.Log)
return []string{ansi.Faint(indent(string(raw), "\t"))}
}
func (v *logView) category() logCategory {
switch logType := v.GetType(); {
case strings.HasPrefix(logType, "s"):
return logCategorySuccess
case strings.HasPrefix(logType, "w"):
return logCategoryWarning
case strings.HasPrefix(logType, "f"):
return logCategoryFailure
default:
return logCategoryUnknown
}
}
func (v *logView) typeDesc() (typ, desc string) {
chunks := strings.Split(v.TypeName(), "(")
// NOTE(cyx): Some logs don't have a typ at all -- for those we'll
// provide some indicator that it's empty so it's not as surprising.
typ = chunks[0]
if typ == "" {
typ = "..."
}
typ = truncate(chunks[0], 23)
if len(chunks) == 2 {
desc = strings.TrimSuffix(chunks[1], ")")
}
desc = fmt.Sprintf("%s %s", desc, auth0.StringValue(v.Description))
switch v.category() {
case logCategorySuccess:
typ = ansi.Green(typ)
case logCategoryFailure:
typ = ansi.BrightRed(typ)
case logCategoryWarning:
typ = ansi.BrightYellow(typ)
default:
typ = ansi.Faint(typ)
}
return typ, desc
}
func (r *Renderer) LogList(logs []*management.Log, silent bool) {
resource := "logs"
r.Heading(resource)
if len(logs) == 0 {
r.EmptyState(resource)
r.Infof("To generate logs, run a test command like 'auth0 test login' or 'auth0 test token'")
return
}
var res []View
for _, l := range logs {
res = append(res, &logView{Log: l, silent: silent, raw: l})
}
r.Results(res)
}
func (r *Renderer) LogTail(logs []*management.Log, ch <-chan []*management.Log, silent bool) {
r.Heading("logs")
var res []View
for _, l := range logs {
res = append(res, &logView{Log: l, silent: silent, raw: l})
}
viewChan := make(chan View)
go func() {
defer close(viewChan)
for list := range ch {
for _, l := range list {
viewChan <- &logView{Log: l, silent: silent, raw: l}
}
}
}()
r.Stream(res, viewChan)
}