From 7f0bf7983ea8e03d73c8c570adc9e212ee4fd958 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Blaise?= <clementblaise@me.com> Date: Fri, 19 Feb 2021 11:05:11 +0100 Subject: [PATCH] Add askForConfirmation on play --- cmd/play.go | 7 +++++++ cmd/shared.go | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/cmd/play.go b/cmd/play.go index 3eea606..2e6457d 100644 --- a/cmd/play.go +++ b/cmd/play.go @@ -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) diff --git a/cmd/shared.go b/cmd/shared.go index fe1add7..a12d74c 100644 --- a/cmd/shared.go +++ b/cmd/shared.go @@ -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" ) @@ -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 + } + } +}