Skip to content

Commit

Permalink
Add askForConfirmation on play
Browse files Browse the repository at this point in the history
  • Loading branch information
clementblaise committed Feb 19, 2021
1 parent 581e6f4 commit 7f0bf79
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cmd/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ var playCmd = &cobra.Command{
Use: "play",
Short: "Run ansible-playbook command",
Long: `TODO...`,
PreRun: func(cmd *cobra.Command, args []string) {
curr, _ := os.Getwd()
generate(cmd, args, curr)
if !askForConfirmation("Do you confirm ?") {
log.Fatal("canceled...")
}
},
Run: func(cmd *cobra.Command, args []string) {
path, _ := os.Getwd()
play(cmd, args, path)
Expand Down
24 changes: 24 additions & 0 deletions cmd/shared.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package cmd

import (
"bufio"
"fmt"
"github.com/ca-gip/dploy/internal/ansible"
"github.com/ca-gip/dploy/internal/utils"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
"strings"
)

Expand Down Expand Up @@ -136,3 +139,24 @@ func tagsCompletion(toComplete string, path string, playbookPath string) ([]stri

return utils.AppendPrefixOnSlice(remainder, matches), cobra.ShellCompDirectiveDefault
}

func askForConfirmation(s string) bool {
reader := bufio.NewReader(os.Stdin)

for {
fmt.Printf("%s [y/n]: ", s)

response, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}

response = strings.ToLower(strings.TrimSpace(response))

if response == "y" || response == "yes" {
return true
} else if response == "n" || response == "no" {
return false
}
}
}

0 comments on commit 7f0bf79

Please sign in to comment.