Add confirmation before deleting a project or a sdk.
authorSebastien Douheret <sebastien.douheret@iot.bzh>
Thu, 4 Jan 2018 23:44:24 +0000 (00:44 +0100)
committerSebastien Douheret <sebastien.douheret@iot.bzh>
Thu, 4 Jan 2018 23:44:28 +0000 (00:44 +0100)
Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
cmd-projects.go
cmd-sdks.go
utils.go

index 6e7241a..713e3f8 100644 (file)
@@ -92,6 +92,10 @@ func initCmdProjects(cmdDef *[]cli.Command) {
                                                Usage:  "project id",
                                                EnvVar: "XDS_PROJECT_ID",
                                        },
+                                       cli.BoolFlag{
+                                               Name:  "force, f",
+                                               Usage: "remove confirmation prompt before removal",
+                                       },
                                },
                        },
                        {
@@ -209,6 +213,12 @@ func projectsRemove(ctx *cli.Context) error {
                return cli.NewExitError("id parameter or option must be set", 1)
        }
 
+       if !ctx.Bool("force") {
+               if !Confirm("Do you permanently remove project id '" + id + "' [yes/No] ? ") {
+                       return nil
+               }
+       }
+
        if err := HTTPCli.Delete("/projects/"+id, &res); err != nil {
                return cli.NewExitError(err, 1)
        }
index e140856..f34d8b1 100644 (file)
@@ -102,6 +102,10 @@ func initCmdSdks(cmdDef *[]cli.Command) {
                                                Usage:  "sdk id to un-install",
                                                EnvVar: "XDS_SDK_ID",
                                        },
+                                       cli.BoolFlag{
+                                               Name:  "force, f",
+                                               Usage: "remove confirmation prompt before removal",
+                                       },
                                },
                        },
                        {
@@ -289,6 +293,12 @@ func sdksUnInstall(ctx *cli.Context) error {
                return cli.NewExitError("id parameter or option must be set", 1)
        }
 
+       if !ctx.Bool("force") {
+               if !Confirm("Do you permanently remove SDK id '" + id + "' [yes/No] ? ") {
+                       return nil
+               }
+       }
+
        delSdk := xaapiv1.SDK{}
        url := XdsServerComputeURL("/sdks/" + id)
        if err := HTTPCli.Delete(url, &delSdk); err != nil {
index 65b67d3..3d01346 100644 (file)
--- a/utils.go
+++ b/utils.go
@@ -22,6 +22,7 @@ import (
        "encoding/json"
        "fmt"
        "strconv"
+       "strings"
 
        "github.com/franciscocpg/reflectme"
        "github.com/iotbzh/xds-agent/lib/xaapiv1"
@@ -115,3 +116,12 @@ func GetID(ctx *cli.Context) string {
        }
        return id
 }
+
+// Confirm Return true when user answer 'y' or 'yes' to a question
+func Confirm(question string) bool {
+       var answer string
+       fmt.Print(question)
+       fmt.Scanln(&answer)
+       ans := strings.ToLower(strings.TrimSpace(answer))
+       return (ans == "y" || ans == "yes")
+}