From 615fb1bfa0774bda926566bd24cd49a24091ded1 Mon Sep 17 00:00:00 2001 From: Cyril David Date: Tue, 26 Jan 2021 12:21:36 -0800 Subject: [PATCH] WIP: Logs: tweak formatting --- internal/display/logs.go | 123 +++++++++++++++++++++++++-------------- 1 file changed, 79 insertions(+), 44 deletions(-) diff --git a/internal/display/logs.go b/internal/display/logs.go index 87ae00d71..ef395e7fa 100644 --- a/internal/display/logs.go +++ b/internal/display/logs.go @@ -1,59 +1,94 @@ package display import ( - "fmt" - "github.com/logrusorgru/aurora" "strings" - "time" + + "github.com/auth0/auth0-cli/internal/ansi" + "github.com/logrusorgru/aurora" "gopkg.in/auth0.v5/management" ) -func (r *Renderer) LogList(logs []*management.Log, noColor bool) { - for _, l := range logs { - logType := l.GetType() - if !noColor { - // colorize the event type field based on whether it's a success or failure - if strings.HasPrefix(logType, "s") { - logType = aurora.Green(logType).String() - } else if strings.HasPrefix(logType, "f") { - logType = aurora.BrightRed(logType).String() - } - } +const ( + logTimeFormat = "2006-01-02T15:04:05+00:00" + notApplicable = "N/A" +) - fmt.Fprintf( - r.ResultWriter, - "[%s] (%s) client_name=%q client_id=%q", - l.Date.Format(time.RFC3339), - logType, - l.GetClientName(), - l.GetClientID(), - ) - - // if userAgent is present in the log, then add it to the output - reqMap, _ := l.Details["request"].(map[string]interface{}) - userAgent, _ := reqMap["userAgent"].(string) - if userAgent != "" { - fmt.Fprintf( - r.ResultWriter, - " user_agent=%q", - userAgent, - ) +var _ View = &logView{} + +type logView struct { + *management.Log +} + +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 { + return dict["connection"].(string) + } else { + return notApplicable } + } else { + return notApplicable + } - // if an error is present in the log, add it to the output - errMap, _ := l.Details["error"].(map[string]interface{}) - errMsg, _ := errMap["message"].(string) - errType, _ := errMap["type"].(string) - if errType != "" || errMsg != "" { - fmt.Fprintf( - r.ResultWriter, - " error_type=%q error_message=%q", - errType, - errMsg, - ) + return notApplicable +} + +func (v *logView) AsTableRow() []string { + typ, desc := typeDescFor(v.Log, false) + + clientName := v.GetClientName() + if clientName == "" { + clientName = "N/A" + } + + return []string{ + typ, + desc, + ansi.Faint(timeAgo(v.GetDate())), + v.getConnection(), + clientName, + } +} + +func typeDescFor(l *management.Log, noColor bool) (typ, desc string) { + chunks := strings.Split(l.TypeName(), "(") + typ = chunks[0] + + if len(chunks) == 2 { + desc = strings.TrimSuffix(chunks[1], ")") + } + + if !noColor { + // colorize the event type field based on whether it's a success or failure + if strings.HasPrefix(l.GetType(), "s") { + typ = aurora.Green(typ).String() + } else if strings.HasPrefix(l.GetType(), "f") { + typ = aurora.BrightRed(typ).String() + } else if strings.HasPrefix(l.GetType(), "w") { + typ = aurora.BrightYellow(typ).String() } + } + + return typ, desc +} - fmt.Fprint(r.ResultWriter, "\n") +func (r *Renderer) LogList(logs []*management.Log, noColor bool) { + var res []View + for _, l := range logs { + res = append(res, &logView{Log: l}) } + + r.Results(res) }