From 82944836ad6da11b02f621baa776e2903982cf06 Mon Sep 17 00:00:00 2001 From: "ning.yougang" Date: Thu, 20 Sep 2018 15:54:10 +0800 Subject: [PATCH] Add confirm when delete or update action As more and more production system uses openwhisk, Users will need some feature to protect their action to be deleted or updated by mistake When environment variable WSK_CLI_PROMPT_ON_CHANGE=true. --- commands/action.go | 17 +++++++++++++++++ commands/flags.go | 1 + wski18n/resources/en_US.all.json | 4 ++++ 3 files changed, 22 insertions(+) diff --git a/commands/action.go b/commands/action.go index 615f56b0d..23fb715a8 100644 --- a/commands/action.go +++ b/commands/action.go @@ -139,6 +139,13 @@ var actionUpdateCmd = &cobra.Command{ return actionParseError(cmd, args, err) } + if os.Getenv("WSK_CLI_PROMPT_ON_CHANGE") == "true" { + if Flags.action.confirm != "yes" && Flags.action.confirm != "y" { + fmt.Fprintf(color.Output, wski18n.T("Please update action using --confirm yes if you really want to update it")) + return nil + } + } + if _, _, err = Client.Actions.Insert(action, true); err != nil { return actionInsertError(action, err) } @@ -335,6 +342,13 @@ var actionDeleteCmd = &cobra.Command{ Client.Namespace = qualifiedName.GetNamespace() + if os.Getenv("WSK_CLI_PROMPT_ON_CHANGE") == "true" { + if Flags.action.confirm != "yes" && Flags.action.confirm != "y" { + fmt.Fprintf(color.Output, wski18n.T("Please delete action using --confirm yes if you really want to update it")) + return nil + } + } + if _, err = Client.Actions.Delete(qualifiedName.GetEntityName()); err != nil { return actionDeleteError(qualifiedName.GetEntityName(), err) } @@ -1269,6 +1283,8 @@ func init() { actionCreateCmd.Flags().StringVar(&Flags.action.web, WEB_FLAG, "", wski18n.T("treat ACTION as a web action, a raw HTTP web action, or as a standard action; yes | true = web action, raw = raw HTTP web action, no | false = standard action")) actionCreateCmd.Flags().StringVar(&Flags.action.websecure, WEB_SECURE_FLAG, "", wski18n.T("secure the web action. where `SECRET` is true, false, or any string. Only valid when the ACTION is a web action")) + actionDeleteCmd.Flags().StringVar(&Flags.action.confirm, "confirm", "no", wski18n.T("you confirm do this operation?yes or y | default:no (environment variable WSK_CLI_PROMPT_ON_CHANGE=true works)")) + actionUpdateCmd.Flags().BoolVar(&Flags.action.native, "native", false, wski18n.T("treat ACTION as native action (zip file provides a compatible executable to run)")) actionUpdateCmd.Flags().StringVar(&Flags.action.docker, "docker", "", wski18n.T("use provided docker image (a path on DockerHub) to run the action")) actionUpdateCmd.Flags().BoolVar(&Flags.action.copy, "copy", false, wski18n.T("treat ACTION as the name of an existing action")) @@ -1284,6 +1300,7 @@ func init() { actionUpdateCmd.Flags().StringVarP(&Flags.common.paramFile, "param-file", "P", "", wski18n.T("`FILE` containing parameter values in JSON format")) actionUpdateCmd.Flags().StringVar(&Flags.action.web, WEB_FLAG, "", wski18n.T("treat ACTION as a web action, a raw HTTP web action, or as a standard action; yes | true = web action, raw = raw HTTP web action, no | false = standard action")) actionUpdateCmd.Flags().StringVar(&Flags.action.websecure, WEB_SECURE_FLAG, "", wski18n.T("secure the web action. where `SECRET` is true, false, or any string. Only valid when the ACTION is a web action")) + actionUpdateCmd.Flags().StringVar(&Flags.action.confirm, "confirm", "no", wski18n.T("you confirm do this operation?yes or y | default:no")) actionInvokeCmd.Flags().StringSliceVarP(&Flags.common.param, "param", "p", []string{}, wski18n.T("parameter values in `KEY VALUE` format")) actionInvokeCmd.Flags().StringVarP(&Flags.common.paramFile, "param-file", "P", "", wski18n.T("`FILE` containing parameter values in JSON format")) diff --git a/commands/flags.go b/commands/flags.go index 223ccfe08..4282ec443 100644 --- a/commands/flags.go +++ b/commands/flags.go @@ -145,6 +145,7 @@ type ActionFlags struct { url bool save bool saveAs string + confirm string } func IsVerbose() bool { diff --git a/wski18n/resources/en_US.all.json b/wski18n/resources/en_US.all.json index 3e7024ec9..df909a802 100644 --- a/wski18n/resources/en_US.all.json +++ b/wski18n/resources/en_US.all.json @@ -1415,6 +1415,10 @@ "id": "treat ACTION as a web action, a raw HTTP web action, or as a standard action; yes | true = web action, raw = raw HTTP web action, no | false = standard action", "translation": "treat ACTION as a web action, a raw HTTP web action, or as a standard action; yes | true = web action, raw = raw HTTP web action, no | false = standard action" }, + { + "id": "you confirm do this operation?yes or y | default:no (environment variable WSK_CLI_PROMPT_ON_CHANGE=true works)", + "translation": "you confirm do this operation?yes or y | default:no (environment variable WSK_CLI_PROMPT_ON_CHANGE=true works)" + }, { "id": "secure the web action. where `SECRET` is true, false, or any string. Only valid when the ACTION is a web action", "translation": "secure the web action. where `SECRET` is true, false, or any string. Only valid when the ACTION is a web action"