Skip to content

Commit

Permalink
chore: improve argocd app delete (argoproj#10160)
Browse files Browse the repository at this point in the history
Signed-off-by: xin.li <[email protected]>
  • Loading branch information
my-git9 authored and ashutosh16 committed Aug 11, 2022
1 parent 0da5899 commit 33d4618
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
13 changes: 4 additions & 9 deletions cmd/argocd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1083,26 +1083,21 @@ 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
}
} else {
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)
Expand Down
19 changes: 19 additions & 0 deletions util/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 33d4618

Please sign in to comment.