Skip to content

Commit

Permalink
[4/4] DXCDT-317: Add confirmation prompt on open editor updates (#603)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught authored Jan 12, 2023
1 parent d2286fe commit 73176f4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
12 changes: 11 additions & 1 deletion internal/cli/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,20 @@ func updateActionCmd(cli *cli) *cobra.Command {
&inputs.Code,
oldAction.GetCode(),
inputs.Name+".*.js",
cli.actionEditorHint,
); err != nil {
return fmt.Errorf("failed to capture input from the editor: %w", err)
}

if !cli.force && canPrompt(cmd) {
var confirmed bool
if err := prompt.AskBool("Do you want to save the action code?", &confirmed, true); err != nil {
return fmt.Errorf("failed to capture prompt input: %w", err)
}
if !confirmed {
return nil
}
}

updatedAction := &management.Action{
SupportedTriggers: oldAction.SupportedTriggers,
}
Expand Down Expand Up @@ -344,6 +353,7 @@ func updateActionCmd(cli *cli) *cobra.Command {
}

cmd.Flags().BoolVar(&cli.json, "json", false, "Output in json format.")
cmd.Flags().BoolVar(&cli.force, "force", false, "Skip confirmation.")
actionName.RegisterStringU(cmd, &inputs.Name, "")
actionCode.RegisterStringU(cmd, &inputs.Code, "")
actionDependency.RegisterStringMapU(cmd, &inputs.Dependencies, nil)
Expand Down
17 changes: 12 additions & 5 deletions internal/cli/email_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/spf13/cobra"

"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/prompt"
)

const (
Expand Down Expand Up @@ -211,11 +212,20 @@ func updateEmailTemplateCmd(cli *cli) *cobra.Command {
&inputs.Body,
oldTemplate.GetBody(),
inputs.Template+".*.liquid",
cli.emailTemplateEditorHint,
); err != nil {
return fmt.Errorf("failed to capture input from the editor: %w", err)
}

if !cli.force && canPrompt(cmd) {
var confirmed bool
if err := prompt.AskBool("Do you want to save the email template body?", &confirmed, true); err != nil {
return fmt.Errorf("failed to capture prompt input: %w", err)
}
if !confirmed {
return nil
}
}

if err := emailTemplateEnabled.AskBoolU(cmd, &inputs.Enabled, oldTemplate.Enabled); err != nil {
return err
}
Expand Down Expand Up @@ -254,6 +264,7 @@ func updateEmailTemplateCmd(cli *cli) *cobra.Command {
}

cmd.Flags().BoolVar(&cli.json, "json", false, "Output in json format.")
cmd.Flags().BoolVar(&cli.force, "force", false, "Skip confirmation.")
emailTemplateBody.RegisterStringU(cmd, &inputs.Body, "")
emailTemplateFrom.RegisterStringU(cmd, &inputs.From, "")
emailTemplateSubject.RegisterStringU(cmd, &inputs.Subject, "")
Expand All @@ -264,10 +275,6 @@ func updateEmailTemplateCmd(cli *cli) *cobra.Command {
return cmd
}

func (c *cli) emailTemplateEditorHint() {
c.renderer.Infof("%s Once you close the editor, the email template will be saved. To cancel, press CTRL+C.", ansi.Faint("Hint:"))
}

func (c *cli) emailTemplatePickerOptions() (pickerOptions, error) {
return emailTemplateOptions, nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (f *Flag) OpenEditorW(cmd *cobra.Command, value *string, defaultValue, file
return openEditorFlag(cmd, f, value, defaultValue, filename, infoFn, tempFileFn, false)
}

func (f *Flag) OpenEditorU(cmd *cobra.Command, value *string, defaultValue string, filename string, infoFn func()) error {
func (f *Flag) OpenEditorU(cmd *cobra.Command, value *string, defaultValue string, filename string) error {
return openEditorFlag(cmd, f, value, defaultValue, filename, nil, nil, true)
}

Expand Down
12 changes: 11 additions & 1 deletion internal/cli/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,21 @@ func updateRuleCmd(cli *cli) *cobra.Command {
&inputs.Script,
oldRule.GetScript(),
oldRule.GetName()+".*.js",
cli.ruleEditorHint,
)
if err != nil {
return fmt.Errorf("failed to capture input from the editor: %w", err)
}

if !cli.force && canPrompt(cmd) {
var confirmed bool
if err := prompt.AskBool("Do you want to save the rule script?", &confirmed, true); err != nil {
return fmt.Errorf("failed to capture prompt input: %w", err)
}
if !confirmed {
return nil
}
}

updatedRule.Enabled = &inputs.Enabled
if inputs.Name != "" {
updatedRule.Name = &inputs.Name
Expand All @@ -381,6 +390,7 @@ func updateRuleCmd(cli *cli) *cobra.Command {
}

cmd.Flags().BoolVar(&cli.json, "json", false, "Output in json format.")
cmd.Flags().BoolVar(&cli.force, "force", false, "Skip confirmation.")
ruleName.RegisterStringU(cmd, &inputs.Name, "")
ruleEnabled.RegisterBool(cmd, &inputs.Enabled, true)
ruleScript.RegisterStringU(cmd, &inputs.Script, "")
Expand Down

0 comments on commit 73176f4

Please sign in to comment.