Skip to content

Commit

Permalink
feat: move appset delete confirmation to new prompt logic
Browse files Browse the repository at this point in the history
Signed-off-by: pashakostohrys <pavel@codefresh.io>
  • Loading branch information
pasha-codefresh committed Nov 6, 2024
1 parent b14f1d1 commit fbb38dc
Showing 1 changed file with 20 additions and 26 deletions.
46 changes: 20 additions & 26 deletions cmd/argocd/commands/applicationset.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"reflect"
"text/tabwriter"

"github.com/argoproj/argo-cd/v2/cmd/argocd/commands/utils"
"github.com/mattn/go-isatty"
"github.com/spf13/cobra"
"google.golang.org/grpc/codes"
Expand All @@ -18,7 +19,6 @@ import (
"github.com/argoproj/argo-cd/v2/pkg/apiclient/applicationset"
arogappsetv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/argoproj/argo-cd/v2/util/argo"
"github.com/argoproj/argo-cd/v2/util/cli"
"github.com/argoproj/argo-cd/v2/util/errors"
"github.com/argoproj/argo-cd/v2/util/grpc"
argoio "github.com/argoproj/argo-cd/v2/util/io"
Expand Down Expand Up @@ -345,45 +345,39 @@ func NewApplicationSetDeleteCommand(clientOpts *argocdclient.ClientOptions) *cob
conn, appIf := headless.NewClientOrDie(clientOpts, c).NewApplicationSetClientOrDie()
defer argoio.Close(conn)
var isTerminal bool = isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd())
var isConfirmAll bool = false
numOfApps := len(args)
promptFlag := c.Flag("yes")
if promptFlag.Changed && promptFlag.Value.String() == "true" {
noPrompt = true
}

var (
confirmAll = false
confirm = false
)

// This is for backward compatibility,
// before we showed the prompts only when condition isTerminal && !noPrompt is true
promptUtil := utils.NewPrompt(isTerminal && !noPrompt)

for _, appSetQualifiedName := range args {
appSetName, appSetNs := argo.ParseFromQualifiedName(appSetQualifiedName, "")

appsetDeleteReq := applicationset.ApplicationSetDeleteRequest{
Name: appSetName,
AppsetNamespace: appSetNs,
}

if isTerminal && !noPrompt {
var lowercaseAnswer string
if numOfApps == 1 {
lowercaseAnswer = cli.AskToProceedS("Are you sure you want to delete '" + appSetQualifiedName + "' and all its Applications? [y/n] ")
} else {
if !isConfirmAll {
lowercaseAnswer = cli.AskToProceedS("Are you sure you want to delete '" + appSetQualifiedName + "' and all its Applications? [y/n/A] where 'A' is to delete all specified ApplicationSets and their Applications without prompting")
if lowercaseAnswer == "a" || lowercaseAnswer == "all" {
lowercaseAnswer = "y"
isConfirmAll = true
}
} else {
lowercaseAnswer = "y"
}
}
if lowercaseAnswer == "y" || lowercaseAnswer == "yes" {
_, err := appIf.Delete(ctx, &appsetDeleteReq)
errors.CheckError(err)
fmt.Printf("applicationset '%s' deleted\n", appSetQualifiedName)
} else {
fmt.Println("The command to delete '" + appSetQualifiedName + "' was cancelled.")
}
} else {
messageForSingle := "Are you sure you want to delete '" + appSetQualifiedName + "' and all its Applications? [y/n] "
messageForAll := "Are you sure you want to delete '" + appSetQualifiedName + "' and all its Applications? [y/n/A] where 'A' is to delete all specified ApplicationSets and their Applications without prompting"
if !confirmAll {
confirm, confirmAll = promptUtil.ConfirmBaseOnCount(messageForSingle, messageForAll, numOfApps)
}
if confirm || confirmAll {
_, err := appIf.Delete(ctx, &appsetDeleteReq)
errors.CheckError(err)
fmt.Printf("applicationset '%s' deleted\n", appSetQualifiedName)
} else {
fmt.Println("The command to delete '" + appSetQualifiedName + "' was cancelled.")
}
}
},
Expand Down

0 comments on commit fbb38dc

Please sign in to comment.