Skip to content

Commit

Permalink
Added batch deletes to actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Christenson II committed Nov 30, 2023
1 parent b0fd941 commit 651dd2e
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 8 deletions.
21 changes: 13 additions & 8 deletions internal/cli/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,14 +358,10 @@ func updateActionCmd(cli *cli) *cobra.Command {
}

func deleteActionCmd(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 action",
Long: "Delete an action.\n\n" +
"To delete interactively, use `auth0 actions delete` with no arguments.\n\n" +
Expand All @@ -375,12 +371,15 @@ 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
if len(args) == 0 {
if err := actionID.Pick(cmd, &inputs.ID, cli.actionPickerOptions); err != nil {
if err := actionID.PickMany(cmd, &ids, cli.actionPickerOptions); 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 @@ -390,7 +389,13 @@ func deleteActionCmd(cli *cli) *cobra.Command {
}

return ansi.Spinner("Deleting action", func() error {
return cli.api.Action.Delete(cmd.Context(), inputs.ID)
var errs []error
for _, id := range ids {
if err := cli.api.Action.Delete(cmd.Context(), id); err != nil {
errs = append(errs, err)
}
}
return errors.Join(errs...)
})
},
}
Expand Down
21 changes: 21 additions & 0 deletions internal/cli/arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,27 @@ func (a *Argument) Pick(cmd *cobra.Command, result *string, fn pickerOptionsFunc
return nil
}

func (a *Argument) PickMany(cmd *cobra.Command, result *[]string, fn pickerOptionsFunc) error {
var opts pickerOptions
err := ansi.Waiting(func() error {
var err error
opts, err = fn(cmd.Context())
return err
})

if err != nil {
return err
}

var values []string
if err := askMultiSelect(a, &values, false, opts.labels()...); err != nil {
return err
}

*result = opts.getValues(values...)
return nil
}

func selectArgument(cmd *cobra.Command, a *Argument, value interface{}, options []string, defaultValue *string) error {
if canPrompt(cmd) {
return _select(a, value, options, defaultValue, false)
Expand Down
10 changes: 10 additions & 0 deletions internal/cli/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ func askPassword(i commandInput, value interface{}, isUpdate bool) error {
return nil
}

func askMultiSelect(i commandInput, value interface{}, isUpdate bool, options ...string) error {
_ = isInputRequired(i, isUpdate) // TODO: handle isRequired

if err := prompt.AskMultiSelect(i.GetLabel(), value, options...); err != nil {
handleInputError(err)
}

return nil
}

func _select(i commandInput, value interface{}, options []string, defaultValue *string, isUpdate bool) error {
isRequired := isInputRequired(i, isUpdate)

Expand Down
12 changes: 12 additions & 0 deletions internal/cli/picker_options.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cli

import "slices"

type pickerOptions []pickerOption

func (p pickerOptions) labels() []string {
Expand All @@ -26,6 +28,16 @@ func (p pickerOptions) getValue(label string) string {
return ""
}

func (p pickerOptions) getValues(labels ...string) []string {
var values []string
for _, o := range p {
if i := slices.Index(labels, o.label); i > -1 {
values = append(values, o.value)
}
}
return values
}

type pickerOption struct {
label string
value string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,20 @@ func TestPickerOptions(t *testing.T) {

assert.Equal(t, want, got)
})

t.Run("return the values for a given set of labels", func(t *testing.T) {
options := pickerOptions{pickerOption{label: "Foo", value: "0"}, pickerOption{label: "Bar", value: "1"}, pickerOption{label: "Baz", value: "2"}}
want := []string{"0", "2"}
got := options.getValues("Foo", "Fizz")

assert.Equal(t, want, got)
})

t.Run("returns empty values for a given set of non-existant labels", func(t *testing.T) {
options := pickerOptions{pickerOption{label: "Foo", value: "0"}, pickerOption{label: "Bar", value: "1"}, pickerOption{label: "Baz", value: "2"}}
want := []string(nil)
got := options.getValues("Buz")

assert.Equal(t, want, got)
})
}
11 changes: 11 additions & 0 deletions internal/prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ func askOne(prompt survey.Prompt, response interface{}) error {
return survey.AskOne(prompt, response, stdErrWriter, Icons)
}

func AskMultiSelect(message string, response interface{}, options ...string) error {
prompt := &survey.MultiSelect{
Message: message,
Options: options,
}

err := askOne(prompt, response)

return err
}

func AskBool(message string, value *bool, defaultValue bool) error {
prompt := &survey.Confirm{
Message: message,
Expand Down

0 comments on commit 651dd2e

Please sign in to comment.