diff --git a/cmd/teardown.go b/cmd/teardown.go index 0b5c09d..8fa01d8 100644 --- a/cmd/teardown.go +++ b/cmd/teardown.go @@ -10,11 +10,16 @@ import ( "github.com/stackrox/roxie/internal/component" "github.com/stackrox/roxie/internal/deployer" "github.com/stackrox/roxie/internal/env" + "github.com/stackrox/roxie/internal/helpers" "github.com/stackrox/roxie/internal/k8s" log "github.com/stackrox/roxie/internal/logger" "github.com/stackrox/roxie/internal/manifest" ) +var ( + force bool +) + func newTeardownCmd(settings *deployer.Config) *cobra.Command { cmd := &cobra.Command{ Use: "teardown [component]", @@ -37,6 +42,8 @@ func newTeardownCmd(settings *deployer.Config) *cobra.Command { }), ) + cmd.Flags().BoolVarP(&force, "force", "f", false, "Skip confirmation prompt") + return cmd } @@ -50,6 +57,16 @@ func runTeardown(cmd *cobra.Command, args []string) error { return err } + if !force && env.RunningInteractively { + confirmed, err := helpers.WaitForConfirmation("About to teardown %s on %s, continue?", components, env.GetCurrentContext()) + if err != nil { + return err + } + if !confirmed { + log.Info("Aborting teardown") + return nil + } + } log.Infof("Tearing down %s", components) if dryRun { diff --git a/internal/helpers/helpers.go b/internal/helpers/helpers.go index 4c08605..1339698 100644 --- a/internal/helpers/helpers.go +++ b/internal/helpers/helpers.go @@ -1,13 +1,18 @@ package helpers import ( + "bufio" "bytes" "fmt" + "io" "os" "os/exec" "reflect" + "strings" "gopkg.in/yaml.v3" + + "github.com/fatih/color" ) // RunCommandWithOutput executes a command and captures stdout/stderr @@ -150,3 +155,19 @@ func IsNil(i interface{}) bool { } return false } + +// Wait for user confirmation +func WaitForConfirmation(f string, a ...any) (bool, error) { + yellow := color.New(color.FgYellow, color.Bold) + reader := bufio.NewReader(os.Stdin) + prompt := fmt.Sprintf(f, a...) + yellow.Printf("%s [y/N]: ", prompt) + response, err := reader.ReadString('\n') + if err == io.EOF { + return false, nil + } else if err != nil { + return false, err + } + response = strings.ToLower(strings.TrimSpace(response)) + return response == "y" || response == "yes", nil +}