Skip to content

Commit

Permalink
feat: confirm envd destroy when path and name are both empty (#1568)
Browse files Browse the repository at this point in the history
* feat: fail `envd destroy` when path and name are both empty

* fix: display user confirmation innstead of failing destroy

* feat: additional comment for better clarity
  • Loading branch information
hxu296 authored Apr 23, 2023
1 parent ac0dd7e commit e2f9e6b
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions pkg/app/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
package app

import (
"bufio"
"fmt"
"os"
"path/filepath"

"github.com/cockroachdb/errors"
"github.com/mattn/go-isatty"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"

Expand Down Expand Up @@ -51,19 +55,36 @@ var CommandDestroy = &cli.Command{
Action: destroy,
}

// Prompts the user to confirm an operation with [Y/n].
// If the output is not tty, it will return false automatically.
func confirm(prompt string) bool {
isTerminal := isatty.IsTerminal(os.Stdout.Fd())
if !isTerminal {
return false
}

reader := bufio.NewReader(os.Stdin)
fmt.Printf("%s [Y/n] ", prompt)
response, err := reader.ReadString('\n')
if err != nil {
return false
}

response = response[:len(response)-1] // Remove newline character
return response == "y" || response == "Y" || response == "yes" || response == "Yes"
}

func destroy(clicontext *cli.Context) error {
path := clicontext.Path("path")
name := clicontext.String("name")
if path != "" && name != "" {
return errors.New("Cannot specify --path and --name at the same time.")
}
if path == "" && name == "" {
path = "."
}

var ctrName string
if name != "" {
ctrName = name
} else {
} else if path != "" {
buildContext, err := filepath.Abs(path)
if err != nil {
return errors.Wrap(err, "failed to get absolute path of the build context")
Expand All @@ -72,7 +93,22 @@ func destroy(clicontext *cli.Context) error {
if err != nil {
return errors.Wrap(err, "failed to create an env name")
}
} else {
// Both path and name are empty
// Destroy the environment in the current directory only if user confirms
buildContext, err := filepath.Abs(".")
if err != nil {
return errors.Wrap(err, "failed to get absolute path of the build context")
}
ctrName, err = buildutil.CreateEnvNameFromDir(buildContext)
if err != nil {
return errors.Wrap(err, "failed to create an env name")
}
if !confirm(fmt.Sprintf("Are you sure you want to destroy container %s in the current directory?", ctrName)) {
return nil
}
}

context, err := home.GetManager().ContextGetCurrent()
if err != nil {
return errors.Wrap(err, "failed to get the current context")
Expand Down

0 comments on commit e2f9e6b

Please sign in to comment.