From 729c9a83a110cb306c7589e2fcf8a77338324df5 Mon Sep 17 00:00:00 2001 From: my-git9 Date: Tue, 9 Aug 2022 05:16:22 +0800 Subject: [PATCH] chore: improve argocd app delete (#10160) Signed-off-by: xin.li --- cmd/argocd/commands/app.go | 13 ++++--------- util/cli/cli.go | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/cmd/argocd/commands/app.go b/cmd/argocd/commands/app.go index 4f688f3777b25..ce14642ee0523 100644 --- a/cmd/argocd/commands/app.go +++ b/cmd/argocd/commands/app.go @@ -1083,18 +1083,13 @@ func NewApplicationDeleteCommand(clientOpts *argocdclient.ClientOptions) *cobra. appDeleteReq.PropagationPolicy = &propagationPolicy } if cascade && isTerminal && !noPrompt { - var confirmAnswer string = "n" var lowercaseAnswer string if numOfApps == 1 { - fmt.Println("Are you sure you want to delete '" + appName + "' and all its resources? [y/n]") - fmt.Scan(&confirmAnswer) - lowercaseAnswer = strings.ToLower(confirmAnswer) + lowercaseAnswer = cli.AskToProceedS("Are you sure you want to delete '" + appName + "' and all its resources? [y/n]") } else { if !isConfirmAll { - fmt.Println("Are you sure you want to delete '" + appName + "' and all its resources? [y/n/A] where 'A' is to delete all specified apps and their resources without prompting") - fmt.Scan(&confirmAnswer) - lowercaseAnswer = strings.ToLower(confirmAnswer) - if lowercaseAnswer == "a" || lowercaseAnswer == "all" { + lowercaseAnswer = cli.AskToProceedS("Are you sure you want to delete '" + appName + "' and all its resources? [y/n/A] where 'A' is to delete all specified apps and their resources without prompting") + if lowercaseAnswer == "a" { lowercaseAnswer = "y" isConfirmAll = true } @@ -1102,7 +1097,7 @@ func NewApplicationDeleteCommand(clientOpts *argocdclient.ClientOptions) *cobra. lowercaseAnswer = "y" } } - if lowercaseAnswer == "y" || lowercaseAnswer == "yes" { + if lowercaseAnswer == "y" { _, err := appIf.Delete(ctx, &appDeleteReq) errors.CheckError(err) fmt.Printf("application '%s' deleted\n", appName) diff --git a/util/cli/cli.go b/util/cli/cli.go index 83c8076a97743..8b4d014c921ff 100644 --- a/util/cli/cli.go +++ b/util/cli/cli.go @@ -127,6 +127,25 @@ func AskToProceed(message string) bool { } } +// AskToProceedS prompts the user with a message (typically a yes, no or all question) and returns string +// "a", "y" or "n". +func AskToProceedS(message string) string { + for { + fmt.Print(message) + reader := bufio.NewReader(os.Stdin) + proceedRaw, err := reader.ReadString('\n') + errors.CheckError(err) + switch strings.ToLower(strings.TrimSpace(proceedRaw)) { + case "y", "yes": + return "y" + case "n", "no": + return "n" + case "a", "all": + return "a" + } + } +} + // ReadAndConfirmPassword is a helper to read and confirm a password from stdin func ReadAndConfirmPassword(username string) (string, error) { for {