Skip to content

Commit

Permalink
[A0CLI-22] Additional options to logs command (#30)
Browse files Browse the repository at this point in the history
* feat: support logs -n and -f options

* feat: support logs --no-color option

* Update copy for logs -f flag

Co-authored-by: Cyril David <[email protected]>
  • Loading branch information
shushen and cyx authored Jan 26, 2021
1 parent 44fe6c1 commit 6b073d8
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 40 deletions.
100 changes: 70 additions & 30 deletions internal/cli/logs.go
Original file line number Diff line number Diff line change
@@ -1,56 +1,96 @@
package cli

import (
"fmt"
"github.com/spf13/cobra"
"gopkg.in/auth0.v5/management"
"sort"
"time"
)

func logsCmd(cli *cli) *cobra.Command {
cmd := &cobra.Command{
Use: "logs",
Short: "manage resources for logs.",
func getLatestLogs(cli *cli, n int) (result []*management.Log, err error) {
var list []*management.Log
page := 0
perPage := 100
var count int
if count = n; n > 1000 {
// Pagination max out at 1000 entries in total
// https://auth0.com/docs/logs/retrieve-log-events-using-mgmt-api#limitations
count = 1000
}
if perPage > count {
perPage = count
}
for count > len(result) {
list, err = cli.api.Log.List(
management.Parameter("sort", "date:-1"),
management.Parameter("page", fmt.Sprintf("%d", page)),
management.Parameter("per_page", fmt.Sprintf("%d", perPage)),
)
if err != nil {
return
}

cmd.SetUsageTemplate(resourceUsageTemplate())
cmd.AddCommand(tailLogsCmd(cli))
sort.Slice(list, func(i, j int) bool {
return list[i].GetDate().Before(list[j].GetDate())
})
result = append(list, result...)
if len(list) < perPage {
// We've got all
break
}
page++
}

return cmd
return
}

func tailLogsCmd(cli *cli) *cobra.Command {
func logsCmd(cli *cli) *cobra.Command {
var numberOfLogs int
var follow bool
var noColor bool
cmd := &cobra.Command{
Use: "tail",
Short: "Tail your log events as they are happening",
Long: `$ auth0 logs tail
Tail your logs as they are happening.
Use: "logs",
Short: "show the tenant logs",
Long: `$ auth0 logs
Show the tenant logs.
`,
RunE: func(cmd *cobra.Command, args []string) error {
list, err := cli.api.Log.List(management.Parameter("sort", "date:1"))
lastLogID := ""
list, err := getLatestLogs(cli, numberOfLogs)
if err != nil {
return err
}
fromLogId := ""
for {
if len(list) > 0 {
cli.renderer.LogList(list)
fromLogId = list[len(list)-1].GetLogID()
}
list, err = cli.api.Log.List(
management.Parameter("from", fromLogId),
management.Parameter("take", "100"),
)
if err != nil {
return err
}

if len(list) < 90 {
// Not a lot is happening, sleep on it
time.Sleep(1 * time.Second)
if len(list) > 0 {
lastLogID = list[len(list)-1].GetLogID()
cli.renderer.LogList(list, noColor)
}
if follow {
for {
list, err = cli.api.Log.List(
management.Parameter("from", lastLogID),
management.Parameter("take", "100"),
)
if err != nil {
return err
}
if len(list) > 0 {
cli.renderer.LogList(list, noColor)
lastLogID = list[len(list)-1].GetLogID()
}
if len(list) < 90 {
// Not a lot is happening, sleep on it
time.Sleep(1 * time.Second)
}
}
}
return nil
},
}

cmd.Flags().IntVarP(&numberOfLogs, "num-entries", "n", 100, "the number of log entries to print")
cmd.Flags().BoolVarP(&follow, "follow", "f", false, "Specify if the logs should be streamed.")
cmd.Flags().BoolVarP(&noColor, "no-color", "", false, "turn off colored print")

return cmd
}
20 changes: 10 additions & 10 deletions internal/display/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ package display

import (
"fmt"
"github.com/logrusorgru/aurora"
"strings"
"time"

"github.com/logrusorgru/aurora"
"gopkg.in/auth0.v5/management"
)

func (r *Renderer) LogList(logs []*management.Log) {
func (r *Renderer) LogList(logs []*management.Log, noColor bool) {
for _, l := range logs {
// colorize the event type field based on whether it's a success or failure
var logType aurora.Value
if t := l.GetType(); strings.HasPrefix(t, "s") {
logType = aurora.Green(t)
} else if strings.HasPrefix(t, "f") {
logType = aurora.BrightRed(t)
} else {
logType = aurora.Reset(t)
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()
}
}

fmt.Fprintf(
Expand Down

0 comments on commit 6b073d8

Please sign in to comment.