Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace client-id with filter in logs commands #310

Merged
merged 2 commits into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions docs/auth0_logs_list.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## auth0 logs list

Show the tenant logs allowing to filter by Client Id.
Show the tenant logs allowing to filter using Lucene query syntax.

```
auth0 logs list [flags]
Expand All @@ -10,16 +10,21 @@ auth0 logs list [flags]

```
auth0 logs list
auth0 logs list --client-id <id>
auth0 logs list --filter "client_id:<client-id>"
auth0 logs list --filter "client_name:<client-name>"
auth0 logs list --filter "user_id:<user-id>"
auth0 logs list --filter "user_name:<user-name>"
auth0 logs list --filter "ip:<ip>"
auth0 logs list --filter "type:f" # See the full list of type codes at https://auth0.com/docs/logs/log-event-type-codes
auth0 logs ls -n 100
```

### Flags

```
-c, --client-id string Client Id of an Auth0 application to filter the logs.
-h, --help help for list
-n, --number int Number of log entries to show. (default 100)
-f, --filter string Filter in Lucene query syntax. See https://auth0.com/docs/logs/log-search-query-syntax for more details.
-h, --help help for list
-n, --number int Number of log entries to show. (default 100)
```

### Flags inherited from parent commands
Expand Down
15 changes: 10 additions & 5 deletions docs/auth0_logs_tail.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## auth0 logs tail

Tail the tenant logs allowing to filter by Client ID.
Tail the tenant logs allowing to filter using Lucene query syntax.

```
auth0 logs tail [flags]
Expand All @@ -10,16 +10,21 @@ auth0 logs tail [flags]

```
auth0 logs tail
auth0 logs tail --client-id <id>
auth0 logs tail --filter "client_id:<client-id>"
auth0 logs tail --filter "client_name:<client-name>"
auth0 logs tail --filter "user_id:<user-id>"
auth0 logs tail --filter "user_name:<user-name>"
auth0 logs tail --filter "ip:<ip>"
auth0 logs tail --filter "type:f" # See the full list of type codes at https://auth0.com/docs/logs/log-event-type-codes
auth0 logs tail -n 100
```

### Flags

```
-c, --client-id string Client Id of an Auth0 application to filter the logs.
-h, --help help for tail
-n, --number int Number of log entries to show. (default 100)
-f, --filter string Filter in Lucene query syntax. See https://auth0.com/docs/logs/log-search-query-syntax for more details.
-h, --help help for tail
-n, --number int Number of log entries to show. (default 100)
```

### Flags inherited from parent commands
Expand Down
2 changes: 1 addition & 1 deletion docs/auth0_users_search.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ auth0 users search -q name -s "name:1"

```
-h, --help help for search
-q, --query string Query in Lucene query string syntax. See https://auth0.com/docs/users/user-search/user-search-query-syntax for more details.
-q, --query string Query in Lucene query syntax. See https://auth0.com/docs/users/user-search/user-search-query-syntax for more details.
-s, --sort string Field to sort by. Use 'field:order' where 'order' is '1' for ascending and '-1' for descending. e.g. 'created_at:1'.
```

Expand Down
50 changes: 30 additions & 20 deletions internal/cli/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
)

var (
logsClientID = Flag{
Name: "Client ID",
LongForm: "client-id",
ShortForm: "c",
Help: "Client Id of an Auth0 application to filter the logs.",
logsFilter = Flag{
Name: "Filter",
LongForm: "filter",
ShortForm: "f",
Help: "Filter in Lucene query syntax. See https://auth0.com/docs/logs/log-search-query-syntax for more details.",
}

logsNum = Flag{
Expand Down Expand Up @@ -42,7 +42,7 @@ func logsCmd(cli *cli) *cobra.Command {

func listLogsCmd(cli *cli) *cobra.Command {
var inputs struct {
ClientID string
Filter string
Num int
}

Expand All @@ -51,12 +51,17 @@ func listLogsCmd(cli *cli) *cobra.Command {
Aliases: []string{"ls"},
Args: cobra.MaximumNArgs(1),
Short: "Show the application logs",
Long: "Show the tenant logs allowing to filter by Client Id.",
Long: "Show the tenant logs allowing to filter using Lucene query syntax.",
Example: `auth0 logs list
auth0 logs list --client-id <id>
auth0 logs list --filter "client_id:<client-id>"
auth0 logs list --filter "client_name:<client-name>"
auth0 logs list --filter "user_id:<user-id>"
auth0 logs list --filter "user_name:<user-name>"
auth0 logs list --filter "ip:<ip>"
auth0 logs list --filter "type:f" # See the full list of type codes at https://auth0.com/docs/logs/log-event-type-codes
auth0 logs ls -n 100`,
RunE: func(cmd *cobra.Command, args []string) error {
list, err := getLatestLogs(cli, inputs.Num, inputs.ClientID)
list, err := getLatestLogs(cli, inputs.Num, inputs.Filter)
if err != nil {
return fmt.Errorf("An unexpected error occurred while getting logs: %v", err)
}
Expand All @@ -67,28 +72,33 @@ auth0 logs ls -n 100`,
},
}

logsClientID.RegisterString(cmd, &inputs.ClientID, "")
logsFilter.RegisterString(cmd, &inputs.Filter, "")
logsNum.RegisterInt(cmd, &inputs.Num, 100)
return cmd
}

func tailLogsCmd(cli *cli) *cobra.Command {
var inputs struct {
ClientID string
Filter string
Num int
}

cmd := &cobra.Command{
Use: "tail",
Args: cobra.MaximumNArgs(1),
Short: "Tail the tenant logs",
Long: "Tail the tenant logs allowing to filter by Client ID.",
Long: "Tail the tenant logs allowing to filter using Lucene query syntax.",
Example: `auth0 logs tail
auth0 logs tail --client-id <id>
auth0 logs tail --filter "client_id:<client-id>"
auth0 logs tail --filter "client_name:<client-name>"
auth0 logs tail --filter "user_id:<user-id>"
auth0 logs tail --filter "user_name:<user-name>"
auth0 logs tail --filter "ip:<ip>"
auth0 logs tail --filter "type:f" # See the full list of type codes at https://auth0.com/docs/logs/log-event-type-codes
auth0 logs tail -n 100`,
RunE: func(cmd *cobra.Command, args []string) error {
lastLogID := ""
list, err := getLatestLogs(cli, inputs.Num, inputs.ClientID)
list, err := getLatestLogs(cli, inputs.Num, inputs.Filter)
if err != nil {
return fmt.Errorf("An unexpected error occurred while getting logs: %v", err)
}
Expand Down Expand Up @@ -119,8 +129,8 @@ auth0 logs tail -n 100`,
management.Parameter("sort", "date:-1"),
}

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

list, err = cli.api.Log.List(queryParams...)
Expand All @@ -147,12 +157,12 @@ auth0 logs tail -n 100`,
},
}

logsClientID.RegisterString(cmd, &inputs.ClientID, "")
logsFilter.RegisterString(cmd, &inputs.Filter, "")
logsNum.RegisterInt(cmd, &inputs.Num, 100)
return cmd
}

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

Expand All @@ -167,8 +177,8 @@ func getLatestLogs(cli *cli, n int, clientID string) ([]*management.Log, error)
management.Parameter("page", fmt.Sprintf("%d", page)),
management.Parameter("per_page", fmt.Sprintf("%d", perPage))}

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

return cli.api.Log.List(queryParams...)
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var (
Name: "Query",
LongForm: "query",
ShortForm: "q",
Help: "Query in Lucene query string syntax. See https://auth0.com/docs/users/user-search/user-search-query-syntax for more details.",
Help: "Query in Lucene query syntax. See https://auth0.com/docs/users/user-search/user-search-query-syntax for more details.",
IsRequired: true,
}
userSort = Flag{
Expand Down
8 changes: 4 additions & 4 deletions internal/display/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ func (v *logView) AsTableRow() []string {

conn := v.getConnection()
if conn == notApplicable {
conn = ansi.Faint(truncate(conn, 25))
conn = ansi.Faint(truncate(conn, 20))
} else {
conn = truncate(conn, 25)
conn = truncate(conn, 20)
}

return []string{
typ,
truncate(desc, 50),
truncate(desc, 54),
ansi.Faint(truncate(timeAgo(v.GetDate()), 17)),
conn,
clientName,
Expand Down Expand Up @@ -118,7 +118,7 @@ func (v *logView) typeDesc() (typ, desc string) {
typ = "..."
}

typ = truncate(chunks[0], 22)
typ = truncate(chunks[0], 23)

if len(chunks) == 2 {
desc = strings.TrimSuffix(chunks[1], ")")
Expand Down