Skip to content

Commit

Permalink
Updates for code review
Browse files Browse the repository at this point in the history
Ensure valid id before deletion process
Normalize messages for deletion
Remove unnecessary path escapes
Continue on read failure instead of attempting delete
Inform user of failed id on batch deletion
Only document at most two ids likely
  • Loading branch information
Michael Christenson II committed Dec 4, 2023
1 parent d6189a8 commit ce1a060
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 74 deletions.
7 changes: 4 additions & 3 deletions internal/cli/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ func deleteActionCmd(cli *cli) *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Aliases: []string{"rm"},
Args: cobra.MinimumNArgs(0),
Short: "Delete an action",
Long: "Delete an action.\n\n" +
"To delete interactively, use `auth0 actions delete` with no arguments.\n\n" +
Expand Down Expand Up @@ -391,8 +390,10 @@ func deleteActionCmd(cli *cli) *cobra.Command {
return ansi.Spinner("Deleting action", func() error {
var errs []error
for _, id := range ids {
if err := cli.api.Action.Delete(cmd.Context(), id); err != nil {
errs = append(errs, err)
if id != "" {
if err := cli.api.Action.Delete(cmd.Context(), id); err != nil {
errs = append(errs, err)
}
}
}
return errors.Join(errs...)
Expand Down
18 changes: 10 additions & 8 deletions internal/cli/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,6 @@ func deleteAPICmd(cli *cli) *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Aliases: []string{"rm"},
Args: cobra.MinimumNArgs(0),
Short: "Delete an API",
Long: "Delete an API.\n\n" +
"To delete interactively, use `auth0 apis delete` with no arguments.\n\n" +
Expand All @@ -446,15 +445,18 @@ func deleteAPICmd(cli *cli) *cobra.Command {
}
}

return ansi.Spinner("Deleting API", func() error {
return ansi.Spinner("Deleting API(s)", func() error {
var errs []error
for _, id := range ids {
if _, err := cli.api.ResourceServer.Read(cmd.Context(), url.PathEscape(id)); err != nil {
errs = append(errs, fmt.Errorf("Unable to read API for deletion: %w", err))
}

if err := cli.api.ResourceServer.Delete(cmd.Context(), url.PathEscape(id)); err != nil {
errs = append(errs, fmt.Errorf("Unable to delete API: %w", err))
if id != "" {
if _, err := cli.api.ResourceServer.Read(cmd.Context(), url.PathEscape(id)); err != nil {
errs = append(errs, fmt.Errorf("Unable to delete API (%s): %w", id, err))
continue
}

if err := cli.api.ResourceServer.Delete(cmd.Context(), url.PathEscape(id)); err != nil {
errs = append(errs, fmt.Errorf("Unable to delete API (%s): %w", id, err))
}
}
}
return errors.Join(errs...)
Expand Down
19 changes: 10 additions & 9 deletions internal/cli/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"net/url"
"strings"

"github.com/auth0/go-auth0/management"
Expand Down Expand Up @@ -306,7 +305,6 @@ func deleteAppCmd(cli *cli) *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Aliases: []string{"rm"},
Args: cobra.MinimumNArgs(0),
Short: "Delete an application",
Long: "Delete an application.\n\n" +
"To delete interactively, use `auth0 apps delete` with no arguments.\n\n" +
Expand Down Expand Up @@ -335,15 +333,18 @@ func deleteAppCmd(cli *cli) *cobra.Command {
}
}

return ansi.Spinner("Deleting Application", func() error {
return ansi.Spinner("Deleting Application(s)", func() error {
var errs []error
for _, id := range ids {
if _, err := cli.api.Client.Read(cmd.Context(), url.PathEscape(id)); err != nil {
errs = append(errs, fmt.Errorf("Unable to read application for deletion: %w", err))
}

if err := cli.api.Client.Delete(cmd.Context(), url.PathEscape(id)); err != nil {
errs = append(errs, fmt.Errorf("Unable to delete application: %w", err))
if id != "" {
if _, err := cli.api.Client.Read(cmd.Context(), id); err != nil {
errs = append(errs, fmt.Errorf("Unable to delete application (%s): %w", id, err))
continue
}

if err := cli.api.Client.Delete(cmd.Context(), id); err != nil {
errs = append(errs, fmt.Errorf("Unable to delete application (%s): %w", id, err))
}
}
}
return errors.Join(errs...)
Expand Down
19 changes: 10 additions & 9 deletions internal/cli/custom_domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@ func deleteCustomDomainCmd(cli *cli) *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Aliases: []string{"rm"},
Args: cobra.MinimumNArgs(0),
Short: "Delete a custom domain",
Long: "Delete a custom domain.\n\n" +
"To delete interactively, use `auth0 domains delete` with no arguments.\n\n" +
Expand All @@ -337,8 +336,8 @@ func deleteCustomDomainCmd(cli *cli) *cobra.Command {
auth0 domains rm
auth0 domains delete <domain-id>
auth0 domains delete <domain-id> --force
auth0 domains delete <domain-id> <domain-id2> <domain-idn>
auth0 domains delete <domain-id> <domain-id2> <domain-idn> --force`,
auth0 domains delete <domain-id> <domain-id2>
auth0 domains delete <domain-id> <domain-id2> --force`,
RunE: func(cmd *cobra.Command, args []string) error {
ids := make([]string, len(args))
if len(args) == 0 {
Expand All @@ -359,12 +358,14 @@ func deleteCustomDomainCmd(cli *cli) *cobra.Command {
return ansi.Spinner("Deleting custom domain", func() error {
var errs []error
for _, id := range ids {
if _, err := cli.api.CustomDomain.Read(cmd.Context(), url.PathEscape(id)); err != nil {
return fmt.Errorf("Unable to read custom domain for deletion: %w", err)
}

if err := cli.api.CustomDomain.Delete(cmd.Context(), url.PathEscape(id)); err != nil {
return fmt.Errorf("Unable to delete custom domain: %w", err)
if id != "" {
if _, err := cli.api.CustomDomain.Read(cmd.Context(), url.PathEscape(id)); err != nil {
return fmt.Errorf("Unable to delete custom domain (%s): %w", id, err)
}

if err := cli.api.CustomDomain.Delete(cmd.Context(), url.PathEscape(id)); err != nil {
return fmt.Errorf("Unable to delete custom domain (%s): %w", id, err)
}
}
}

Expand Down
20 changes: 11 additions & 9 deletions internal/cli/log_streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ func deleteLogStreamCmd(cli *cli) *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Aliases: []string{"rm"},
Args: cobra.MinimumNArgs(0),
Short: "Delete a log stream",
Long: "Delete a log stream.\n\n" +
"To delete interactively, use `auth0 logs streams delete` with no arguments.\n\n" +
Expand All @@ -181,8 +180,8 @@ func deleteLogStreamCmd(cli *cli) *cobra.Command {
auth0 logs streams rm
auth0 logs streams delete <log-stream-id>
auth0 logs streams delete <log-stream-id> --force
auth0 logs streams delete <log-stream-id> <log-stream-id2> <log-stream-idn>
auth0 logs streams delete <log-stream-id> <log-stream-id2> <log-stream-idn> --force`,
auth0 logs streams delete <log-stream-id> <log-stream-id2>
auth0 logs streams delete <log-stream-id> <log-stream-id2> --force`,
RunE: func(cmd *cobra.Command, args []string) error {
ids := make([]string, len(args))
if len(args) == 0 {
Expand All @@ -200,14 +199,17 @@ func deleteLogStreamCmd(cli *cli) *cobra.Command {
}
}

return ansi.Spinner("Deleting Log Stream", func() error {
return ansi.Spinner("Deleting Log Stream(s)", func() error {
var errs []error
for _, id := range ids {
if _, err := cli.api.LogStream.Read(cmd.Context(), id); err != nil {
errs = append(errs, fmt.Errorf("Unable to read log stream for deletion: %w", err))
}
if err := cli.api.LogStream.Delete(cmd.Context(), id); err != nil {
errs = append(errs, fmt.Errorf("Unable to delete log stream: %w", err))
if id != "" {
if _, err := cli.api.LogStream.Read(cmd.Context(), id); err != nil {
errs = append(errs, fmt.Errorf("Unable to delete log stream (%s): %w", id, err))
continue
}
if err := cli.api.LogStream.Delete(cmd.Context(), id); err != nil {
errs = append(errs, fmt.Errorf("Unable to delete log stream (%s): %w", id, err))
}
}
}

Expand Down
18 changes: 10 additions & 8 deletions internal/cli/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,6 @@ func deleteOrganizationCmd(cli *cli) *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Aliases: []string{"rm"},
Args: cobra.MinimumNArgs(0),
Short: "Delete an organization",
Long: "Delete an organization.\n\n" +
"To delete interactively, use `auth0 orgs delete` with no arguments.\n\n" +
Expand Down Expand Up @@ -440,15 +439,18 @@ func deleteOrganizationCmd(cli *cli) *cobra.Command {
}
}

return ansi.Spinner("Deleting organization", func() error {
return ansi.Spinner("Deleting organization(s)", func() error {
var errs []error
for _, id := range ids {
if _, err := cli.api.Organization.Read(cmd.Context(), url.PathEscape(id)); err != nil {
errs = append(errs, fmt.Errorf("Unable to read organization for deletion: %w", err))
}

if err := cli.api.Organization.Delete(cmd.Context(), url.PathEscape(id)); err != nil {
errs = append(errs, fmt.Errorf("Unable to delete organization: %w", err))
if id != "" {
if _, err := cli.api.Organization.Read(cmd.Context(), id); err != nil {
errs = append(errs, fmt.Errorf("Unable to delete organization (%s): %w", id, err))
continue
}

if err := cli.api.Organization.Delete(cmd.Context(), id); err != nil {
errs = append(errs, fmt.Errorf("Unable to delete organization (%s): %w", id, err))
}
}
}
return errors.Join(errs...)
Expand Down
18 changes: 10 additions & 8 deletions internal/cli/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,6 @@ func deleteRoleCmd(cli *cli) *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Aliases: []string{"rm"},
Args: cobra.MinimumNArgs(0),
Short: "Delete a role",
Long: "Delete a role.\n\n" +
"To delete interactively, use `auth0 roles delete`.\n\n" +
Expand Down Expand Up @@ -313,15 +312,18 @@ func deleteRoleCmd(cli *cli) *cobra.Command {
}
}

return ansi.Spinner("Deleting Role", func() error {
return ansi.Spinner("Deleting Role(s)", func() error {
var errs []error
for _, id := range ids {
if _, err := cli.api.Role.Read(cmd.Context(), id); err != nil {
errs = append(errs, fmt.Errorf("Unable to read role for deletion: %w", err))
}

if err := cli.api.Role.Delete(cmd.Context(), id); err != nil {
errs = append(errs, fmt.Errorf("Unable to delete role: %w", err))
if id != "" {
if _, err := cli.api.Role.Read(cmd.Context(), id); err != nil {
errs = append(errs, fmt.Errorf("Unable to delete role (%s): %w", id, err))
continue
}

if err := cli.api.Role.Delete(cmd.Context(), id); err != nil {
errs = append(errs, fmt.Errorf("Unable to delete role (%s): %w", id, err))
}
}
}
return errors.Join(errs...)
Expand Down
18 changes: 10 additions & 8 deletions internal/cli/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ func deleteRuleCmd(cli *cli) *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Aliases: []string{"rm"},
Args: cobra.MinimumNArgs(0),
Short: "Delete a rule",
Long: rulesDeprecationDocumentationText + "Delete a rule.\n\n" +
"To delete interactively, use `auth0 rules delete` with no arguments.\n\n" +
Expand Down Expand Up @@ -280,15 +279,18 @@ func deleteRuleCmd(cli *cli) *cobra.Command {
}
}

return ansi.Spinner("Deleting Rule", func() error {
return ansi.Spinner("Deleting Rule(s)", func() error {
var errs []error
for _, id := range ids {
if _, err := cli.api.Rule.Read(cmd.Context(), id); err != nil {
errs = append(errs, fmt.Errorf("Unable to read rule for deletion: %w", err))
}

if err := cli.api.Rule.Delete(cmd.Context(), id); err != nil {
errs = append(errs, fmt.Errorf("Unable to delete rule: %w", err))
if id != "" {
if _, err := cli.api.Rule.Read(cmd.Context(), id); err != nil {
errs = append(errs, fmt.Errorf("Unable to delete rule (%s): %w", id, err))
continue
}

if err := cli.api.Rule.Delete(cmd.Context(), id); err != nil {
errs = append(errs, fmt.Errorf("Unable to delete rule (%s): %w", id, err))
}
}
}
return errors.Join(errs...)
Expand Down
18 changes: 10 additions & 8 deletions internal/cli/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,6 @@ func deleteUserCmd(cli *cli) *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Aliases: []string{"rm"},
Args: cobra.MinimumNArgs(0),
Short: "Delete a user",
Long: "Delete a user.\n\n" +
"To delete interactively, use `auth0 users delete` with no arguments.\n\n" +
Expand Down Expand Up @@ -389,15 +388,18 @@ func deleteUserCmd(cli *cli) *cobra.Command {
}
}

return ansi.Spinner("Deleting user", func() error {
return ansi.Spinner("Deleting user(s)", func() error {
var errs []error
for _, id := range ids {
if _, err := cli.api.User.Read(cmd.Context(), id); err != nil {
errs = append(errs, fmt.Errorf("Unable to read user for deletion: %w", err))
}

if err := cli.api.User.Delete(cmd.Context(), id); err != nil {
errs = append(errs, fmt.Errorf("Unable to delete user: %w", err))
if id != "" {
if _, err := cli.api.User.Read(cmd.Context(), id); err != nil {
errs = append(errs, fmt.Errorf("Unable to delete user (%s): %w", id, err))
continue
}

if err := cli.api.User.Delete(cmd.Context(), id); err != nil {
errs = append(errs, fmt.Errorf("Unable to delete user (%s): %w", id, err))
}
}
}
return errors.Join(errs...)
Expand Down
9 changes: 5 additions & 4 deletions internal/cli/users_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ func listUserBlocksCmd(cli *cli) *cobra.Command {
func deleteUserBlocksCmd(cli *cli) *cobra.Command {
cmd := &cobra.Command{
Use: "unblock",
Args: cobra.MinimumNArgs(0),
Short: "Remove brute-force protection blocks for a given user",
Long: "Remove brute-force protection blocks for a given user.",
Example: ` auth0 users blocks unblock
Expand All @@ -85,11 +84,13 @@ func deleteUserBlocksCmd(cli *cli) *cobra.Command {
ids = append(ids, args...)
}

return ansi.Spinner("Unblocking user...", func() error {
return ansi.Spinner("Unblocking user(s)...", func() error {
var errs []error
for _, id := range ids {
if err := cli.api.User.Unblock(cmd.Context(), id); err != nil {
errs = append(errs, fmt.Errorf("failed to unblock user with ID %s: %w", id, err))
if id != "" {
if err := cli.api.User.Unblock(cmd.Context(), id); err != nil {
errs = append(errs, fmt.Errorf("failed to unblock user with ID %s: %w", id, err))
}
}
}
return errors.Join(errs...)
Expand Down

0 comments on commit ce1a060

Please sign in to comment.