Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions cmd/teardown.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]",
Expand All @@ -37,6 +42,8 @@ func newTeardownCmd(settings *deployer.Config) *cobra.Command {
}),
)

cmd.Flags().BoolVarP(&force, "force", "f", false, "Skip confirmation prompt")

return cmd
}

Expand All @@ -50,6 +57,16 @@ func runTeardown(cmd *cobra.Command, args []string) error {
return err
}

if !force && env.RunningInteractively {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
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 {
Expand Down
21 changes: 21 additions & 0 deletions internal/helpers/helpers.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}
Loading