Skip to content

Commit

Permalink
Add batch deletions
Browse files Browse the repository at this point in the history
Added batch deletion to actions command
Added batch deletion to apis command
Added batch deletion to apps command
Added batch deletion to custom domains command
Added batch deletion to log streams command
Added batch deletion for organizations command
Added batch deletion for roles command
Added batch deletion for rules command
Added batch deletion for users non-interactive command
Added batch deletion for user blocks non-interactive command
Update required golang minimum version
  • Loading branch information
Michael Christenson II committed Dec 1, 2023
1 parent 651dd2e commit 70123ac
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 115 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/auth0/auth0-cli

go 1.20
go 1.21

require (
github.com/AlecAivazis/survey/v2 v2.3.7
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func deleteActionCmd(cli *cli) *cobra.Command {
auth0 actions delete <action-id>
auth0 actions delete <action-id> --force`,
RunE: func(cmd *cobra.Command, args []string) error {
var ids []string
ids := make([]string, len(args))
if len(args) == 0 {
if err := actionID.PickMany(cmd, &ids, cli.actionPickerOptions); err != nil {
return err
Expand Down
28 changes: 15 additions & 13 deletions internal/cli/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,14 +390,10 @@ func updateAPICmd(cli *cli) *cobra.Command {
}

func deleteAPICmd(cli *cli) *cobra.Command {
var inputs struct {
ID string
}

cmd := &cobra.Command{
Use: "delete",
Aliases: []string{"rm"},
Args: cobra.MaximumNArgs(1),
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 @@ -407,13 +403,15 @@ func deleteAPICmd(cli *cli) *cobra.Command {
auth0 apis delete <api-id|api-audience>
auth0 apis delete <api-id|api-audience> --force`,
RunE: func(cmd *cobra.Command, args []string) error {
var ids []string
if len(args) == 0 {
err := apiID.Pick(cmd, &inputs.ID, cli.apiPickerOptions)
if err != nil {
if err := apiID.PickMany(cmd, &ids, cli.apiPickerOptions); err != nil {
return err
}
} else {
inputs.ID = args[0]
for _, id := range args[0:] {
ids = append(ids, id)
}
}

if !cli.force && canPrompt(cmd) {
Expand All @@ -423,13 +421,17 @@ func deleteAPICmd(cli *cli) *cobra.Command {
}

return ansi.Spinner("Deleting API", func() error {
_, err := cli.api.ResourceServer.Read(cmd.Context(), url.PathEscape(inputs.ID))
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 != nil {
return fmt.Errorf("Unable to delete API: %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))
}
}

return cli.api.ResourceServer.Delete(cmd.Context(), url.PathEscape(inputs.ID))
return errors.Join(errs...)
})
},
}
Expand Down
28 changes: 16 additions & 12 deletions internal/cli/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"net/url"
"strings"

"github.com/auth0/go-auth0/management"
Expand Down Expand Up @@ -302,14 +303,10 @@ func showAppCmd(cli *cli) *cobra.Command {
}

func deleteAppCmd(cli *cli) *cobra.Command {
var inputs struct {
ID string
}

cmd := &cobra.Command{
Use: "delete",
Aliases: []string{"rm"},
Args: cobra.MaximumNArgs(1),
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 All @@ -320,13 +317,16 @@ func deleteAppCmd(cli *cli) *cobra.Command {
auth0 apps delete <app-id>
auth0 apps delete <app-id> --force`,
RunE: func(cmd *cobra.Command, args []string) error {
ids := make([]string, len(args))
if len(args) == 0 {
err := appID.Pick(cmd, &inputs.ID, cli.appPickerOptions())
err := appID.PickMany(cmd, &ids, cli.appPickerOptions())
if err != nil {
return err
}
} else {
inputs.ID = args[0]
for _, id := range args[0:] {
ids = append(ids, id)
}
}

if !cli.force && canPrompt(cmd) {
Expand All @@ -336,13 +336,17 @@ func deleteAppCmd(cli *cli) *cobra.Command {
}

return ansi.Spinner("Deleting Application", func() error {
_, err := cli.api.Client.Read(cmd.Context(), inputs.ID)
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 != nil {
return fmt.Errorf("Unable to delete application: %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))
}
}

return cli.api.Client.Delete(cmd.Context(), inputs.ID)
return errors.Join(errs...)
})
},
}
Expand Down
29 changes: 17 additions & 12 deletions internal/cli/custom_domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"context"
"errors"
"fmt"
"net/url"

Expand Down Expand Up @@ -323,14 +324,10 @@ func updateCustomDomainCmd(cli *cli) *cobra.Command {
}

func deleteCustomDomainCmd(cli *cli) *cobra.Command {
var inputs struct {
ID string
}

cmd := &cobra.Command{
Use: "delete",
Aliases: []string{"rm"},
Args: cobra.MaximumNArgs(1),
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 @@ -341,13 +338,16 @@ func deleteCustomDomainCmd(cli *cli) *cobra.Command {
auth0 domains delete <domain-id>
auth0 domains delete <domain-id> --force`,
RunE: func(cmd *cobra.Command, args []string) error {
ids := make([]string, len(args))
if len(args) == 0 {
err := customDomainID.Pick(cmd, &inputs.ID, cli.customDomainsPickerOptions)
err := customDomainID.PickMany(cmd, &ids, cli.customDomainsPickerOptions)
if err != nil {
return err
}
} else {
inputs.ID = args[0]
for _, id := range args[0:] {
ids = append(ids, id)
}
}

if !cli.force && canPrompt(cmd) {
Expand All @@ -357,13 +357,18 @@ func deleteCustomDomainCmd(cli *cli) *cobra.Command {
}

return ansi.Spinner("Deleting custom domain", func() error {
_, err := cli.api.CustomDomain.Read(cmd.Context(), url.PathEscape(inputs.ID))

if err != nil {
return fmt.Errorf("Unable to delete custom domain: %w", err)
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)
}
}

return cli.api.CustomDomain.Delete(cmd.Context(), url.PathEscape(inputs.ID))
return errors.Join(errs...)
})
},
}
Expand Down
27 changes: 15 additions & 12 deletions internal/cli/log_streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,10 @@ func updateLogStreamCmd(cli *cli) *cobra.Command {
}

func deleteLogStreamCmd(cli *cli) *cobra.Command {
var inputs struct {
ID string
}

cmd := &cobra.Command{
Use: "delete",
Aliases: []string{"rm"},
Args: cobra.MaximumNArgs(1),
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 @@ -186,13 +182,16 @@ func deleteLogStreamCmd(cli *cli) *cobra.Command {
auth0 logs streams delete <log-stream-id>
auth0 logs streams delete <log-stream-id> --force`,
RunE: func(cmd *cobra.Command, args []string) error {
ids := make([]string, len(args))
if len(args) == 0 {
err := logStreamID.Pick(cmd, &inputs.ID, cli.allLogStreamsPickerOptions)
err := logStreamID.PickMany(cmd, &ids, cli.allLogStreamsPickerOptions)
if err != nil {
return err
}
} else {
inputs.ID = args[0]
for _, id := range args[0:] {
ids = append(ids, id)
}
}

if !cli.force && canPrompt(cmd) {
Expand All @@ -202,13 +201,17 @@ func deleteLogStreamCmd(cli *cli) *cobra.Command {
}

return ansi.Spinner("Deleting Log Stream", func() error {
_, err := cli.api.LogStream.Read(cmd.Context(), inputs.ID)

if err != nil {
return fmt.Errorf("Unable to delete log stream: %w", err)
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))
}
}

return cli.api.LogStream.Delete(cmd.Context(), inputs.ID)
return errors.Join(errs...)
})
},
}
Expand Down
27 changes: 15 additions & 12 deletions internal/cli/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,14 +408,10 @@ func updateOrganizationCmd(cli *cli) *cobra.Command {
}

func deleteOrganizationCmd(cli *cli) *cobra.Command {
var inputs struct {
ID string
}

cmd := &cobra.Command{
Use: "delete",
Aliases: []string{"rm"},
Args: cobra.MaximumNArgs(1),
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 All @@ -426,13 +422,16 @@ func deleteOrganizationCmd(cli *cli) *cobra.Command {
auth0 orgs delete <org-id>
auth0 orgs delete <org-id> --force`,
RunE: func(cmd *cobra.Command, args []string) error {
ids := make([]string, len(args))
if len(args) == 0 {
err := organizationID.Pick(cmd, &inputs.ID, cli.organizationPickerOptions)
err := organizationID.PickMany(cmd, &ids, cli.organizationPickerOptions)
if err != nil {
return err
}
} else {
inputs.ID = args[0]
for _, id := range args {
ids = append(ids, id)
}
}

if !cli.force && canPrompt(cmd) {
Expand All @@ -442,13 +441,17 @@ func deleteOrganizationCmd(cli *cli) *cobra.Command {
}

return ansi.Spinner("Deleting organization", func() error {
_, err := cli.api.Organization.Read(cmd.Context(), url.PathEscape(inputs.ID))
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 != nil {
return fmt.Errorf("Unable to delete organization: %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))
}
}

return cli.api.Organization.Delete(cmd.Context(), url.PathEscape(inputs.ID))
return errors.Join(errs...)
})
},
}
Expand Down
27 changes: 15 additions & 12 deletions internal/cli/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,10 @@ func updateRoleCmd(cli *cli) *cobra.Command {
}

func deleteRoleCmd(cli *cli) *cobra.Command {
var inputs struct {
ID string
}

cmd := &cobra.Command{
Use: "delete",
Aliases: []string{"rm"},
Args: cobra.MaximumNArgs(1),
Args: cobra.MinimumNArgs(0),
Short: "Delete a role",
Long: "Delete a role.\n\n" +
"To delete interactively, use `auth0 roles delete`.\n\n" +
Expand All @@ -299,13 +295,16 @@ func deleteRoleCmd(cli *cli) *cobra.Command {
auth0 roles delete <role-id>
auth0 roles delete <role-id> --force`,
RunE: func(cmd *cobra.Command, args []string) error {
ids := make([]string, len(args))
if len(args) == 0 {
err := roleID.Pick(cmd, &inputs.ID, cli.rolePickerOptions)
err := roleID.PickMany(cmd, &ids, cli.rolePickerOptions)
if err != nil {
return err
}
} else {
inputs.ID = args[0]
for _, id := range args {
ids = append(ids, id)
}
}

if !cli.force && canPrompt(cmd) {
Expand All @@ -315,13 +314,17 @@ func deleteRoleCmd(cli *cli) *cobra.Command {
}

return ansi.Spinner("Deleting Role", func() error {
_, err := cli.api.Role.Read(cmd.Context(), inputs.ID)
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 != nil {
return fmt.Errorf("Unable to delete role: %w", err)
if err := cli.api.Role.Delete(cmd.Context(), id); err != nil {
errs = append(errs, fmt.Errorf("Unable to delete role: %w", err))
}
}

return cli.api.Role.Delete(cmd.Context(), inputs.ID)
return errors.Join(errs...)
})
},
}
Expand Down
Loading

0 comments on commit 70123ac

Please sign in to comment.