Skip to content

Commit

Permalink
Add flag to query logs by client ID.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisscott committed Mar 12, 2021
1 parent c95e71a commit 3147cb9
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions internal/cli/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"gopkg.in/auth0.v5/management"
)

func getLatestLogs(cli *cli, n int) ([]*management.Log, error) {
func getLatestLogs(cli *cli, n int, ClientID string) ([]*management.Log, error) {
page := 0
perPage := n

Expand All @@ -20,18 +20,24 @@ func getLatestLogs(cli *cli, n int) ([]*management.Log, error) {
perPage = 1000
}

return cli.api.Log.List(
queryParams := []management.RequestOption{
management.Parameter("sort", "date:-1"),
management.Parameter("page", fmt.Sprintf("%d", page)),
management.Parameter("per_page", fmt.Sprintf("%d", perPage)),
)
management.Parameter("per_page", fmt.Sprintf("%d", perPage))}

if ClientID != "" {
queryParams = append(queryParams, management.Query(fmt.Sprintf(`client_id:"%s"`, ClientID)))
}

return cli.api.Log.List(queryParams...)
}

func logsCmd(cli *cli) *cobra.Command {
var flags struct {
Num int
Follow bool
NoColor bool
Num int
Follow bool
NoColor bool
ClientID string
}

cmd := &cobra.Command{
Expand All @@ -42,7 +48,7 @@ Show the tenant logs.
`,
RunE: func(cmd *cobra.Command, args []string) error {
lastLogID := ""
list, err := getLatestLogs(cli, flags.Num)
list, err := getLatestLogs(cli, flags.Num, flags.ClientID)
if err != nil {
return fmt.Errorf("An unexpected error occurred while getting logs: %v", err)
}
Expand All @@ -68,12 +74,18 @@ Show the tenant logs.
defer close(logsCh)

for {
list, err = cli.api.Log.List(
queryParams := []management.RequestOption{
management.Query(fmt.Sprintf("log_id:[%s TO *]", lastLogID)),
management.Parameter("page", "0"),
management.Parameter("per_page", "100"),
management.Parameter("sort", "date:-1"),
)
}

if flags.ClientID != "" {
queryParams = append(queryParams, management.Query(fmt.Sprintf(`client_id:"%s"`, flags.ClientID)))
}

list, err = cli.api.Log.List(queryParams...)
if err != nil {
cli.renderer.Errorf("An unexpected error occurred while getting logs: %v", err)
return
Expand Down Expand Up @@ -109,6 +121,7 @@ Show the tenant logs.
cmd.Flags().IntVarP(&flags.Num, "num-entries", "n", 100, "the number of log entries to print")
cmd.Flags().BoolVarP(&flags.Follow, "follow", "f", false, "Specify if the logs should be streamed")
cmd.Flags().BoolVar(&flags.NoColor, "no-color", false, "turn off colored print")
cmd.Flags().StringVarP(&flags.ClientID, "client-id", "c", "", "client ID to display logs for")

return cmd
}
Expand Down

0 comments on commit 3147cb9

Please sign in to comment.