Skip to content

Commit

Permalink
WIP: Logs: tweak formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
cyx committed Jan 26, 2021
1 parent df22431 commit 615fb1b
Showing 1 changed file with 79 additions and 44 deletions.
123 changes: 79 additions & 44 deletions internal/display/logs.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 615fb1b

Please sign in to comment.