Skip to content

Commit

Permalink
Merge pull request #73 from aqche/prompt_retries
Browse files Browse the repository at this point in the history
Prompt for 'nuke' again on invalid responses
  • Loading branch information
autero1 authored Oct 11, 2019
2 parents 2325155 + acc6f1c commit 5130262
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions commands/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ func awsNuke(c *cli.Context) error {
}

if !c.Bool("force") {
prompt := "\nAre you sure you want to nuke all listed resources? Enter 'nuke' to confirm: "
proceed, err := confirmationPrompt(prompt)
prompt := "\nAre you sure you want to nuke all listed resources? Enter 'nuke' to confirm (or exit with ^C): "
proceed, err := confirmationPrompt(prompt, 2)
if err != nil {
return err
}
Expand Down Expand Up @@ -217,8 +217,8 @@ func nukeDefaultVpcs(c *cli.Context, regions []string) error {

var proceed bool
if !c.Bool("force") {
prompt := "\nAre you sure you want to nuke all default VPCs? Enter 'nuke' to confirm: "
proceed, err = confirmationPrompt(prompt)
prompt := "\nAre you sure you want to nuke all default VPCs? Enter 'nuke' to confirm (or exit with ^C): "
proceed, err = confirmationPrompt(prompt, 2)
if err != nil {
return err
}
Expand Down Expand Up @@ -246,8 +246,8 @@ func nukeDefaultSecurityGroups(c *cli.Context, regions []string) error {

var proceed bool
if !c.Bool("force") {
prompt := "\nAre you sure you want to nuke the rules in these default security groups ? Enter 'nuke' to confirm: "
proceed, err = confirmationPrompt(prompt)
prompt := "\nAre you sure you want to nuke the rules in these default security groups ? Enter 'nuke' to confirm (or exit with ^C): "
proceed, err = confirmationPrompt(prompt, 2)
if err != nil {
return err
}
Expand All @@ -262,19 +262,27 @@ func nukeDefaultSecurityGroups(c *cli.Context, regions []string) error {
return nil
}

func confirmationPrompt(prompt string) (bool, error) {
func confirmationPrompt(prompt string, maxPrompts int) (bool, error) {
color := color.New(color.FgHiRed, color.Bold)
color.Println("\nTHE NEXT STEPS ARE DESTRUCTIVE AND COMPLETELY IRREVERSIBLE, PROCEED WITH CAUTION!!!")

shellOptions := shell.ShellOptions{Logger: logging.Logger}
input, err := shell.PromptUserForInput(prompt, &shellOptions)

if err != nil {
return false, errors.WithStackTrace(err)
}
// retry prompt on invalid input so user can avoid rescanning all resources
prompts := 0
for prompts < maxPrompts {
input, err := shell.PromptUserForInput(prompt, &shellOptions)

if err != nil {
return false, errors.WithStackTrace(err)
}

if strings.ToLower(input) == "nuke" {
return true, nil
}

if strings.ToLower(input) == "nuke" {
return true, nil
fmt.Printf("Invalid value '%s' was entered.\n", input)
prompts++
}

return false, nil
Expand Down

0 comments on commit 5130262

Please sign in to comment.