-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[A0CLI-22] Additional options to logs command (#30)
* 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
Showing
2 changed files
with
80 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters