Skip to content

Commit

Permalink
Drop command requires user confirmation or -y flag and does not allow…
Browse files Browse the repository at this point in the history
… arguments. (#235)

* Don't allow args and get user confirmation through a flag or input.

* changed long form of confirm flag to "yes". Check for more than 0 args.

* Standardize confirmation hint.

* Add small fixes
  • Loading branch information
Baggerone authored and stanislas-m committed Sep 9, 2018
1 parent d3a552e commit 9e950f8
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions soda/cmd/drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,41 @@ package cmd

import (
"fmt"
"strings"

"bufio"
"os"

"github.com/gobuffalo/pop"
"github.com/spf13/cobra"
)

var all bool
var confirmed bool

var dropCmd = &cobra.Command{
Use: "drop",
Short: "Drops databases for you",
Run: func(cmd *cobra.Command, args []string) {
var err error
if len(args) > 0 {
err = fmt.Errorf("no arguments allowed with the drop database command")
fmt.Println(err)
os.Exit(1)
}

if !confirmed {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Do you really want to drop the database [y/N]? ")
r, _ := reader.ReadString('\n')
r = strings.TrimSpace(r)
if r != "y" && r != "Y" {
fmt.Println("Aborting due to lack of user confirmation.")
os.Exit(0)
}

}

if all {
for _, conn := range pop.Connections {
err = pop.DropDB(conn)
Expand All @@ -31,5 +54,6 @@ var dropCmd = &cobra.Command{

func init() {
dropCmd.Flags().BoolVarP(&all, "all", "a", false, "Drops all of the databases in the database.yml")
dropCmd.Flags().BoolVarP(&confirmed, "yes", "y", false, "Runs without asking the user for confirmation")
RootCmd.AddCommand(dropCmd)
}

0 comments on commit 9e950f8

Please sign in to comment.