From 1ecbaa74c64fd60a2f20f1fe1ebd86ff5a2d5fb7 Mon Sep 17 00:00:00 2001 From: Rita Zerrizuela Date: Fri, 2 Apr 2021 21:01:33 -0300 Subject: [PATCH] Ask for rule enable/disable on 'rules update' --- internal/cli/flags.go | 16 +++++++++++++++- internal/cli/input.go | 5 +++++ internal/cli/rules.go | 11 +++++++---- internal/prompt/prompt.go | 26 ++++++++++++++++++++------ 4 files changed, 47 insertions(+), 11 deletions(-) diff --git a/internal/cli/flags.go b/internal/cli/flags.go index 3c27b42bd..15b0a8115 100644 --- a/internal/cli/flags.go +++ b/internal/cli/flags.go @@ -49,6 +49,14 @@ func (f *Flag) AskManyU(cmd *cobra.Command, value interface{}, defaultValue *str return askManyFlag(cmd, f, value, defaultValue, true) } +func (f *Flag) AskBool(cmd *cobra.Command, value *bool, defaultValue *bool) { + askBoolFlag(cmd, f, value, defaultValue, false) +} + +func (f *Flag) AskBoolU(cmd *cobra.Command, value *bool, defaultValue *bool) { + askBoolFlag(cmd, f, value, defaultValue, true) +} + func (f *Flag) Select(cmd *cobra.Command, value interface{}, options []string, defaultValue *string) error { return selectFlag(cmd, f, value, options, defaultValue, false) } @@ -91,7 +99,7 @@ func (f *Flag) EditorPromptU(cmd *cobra.Command, value *string, initialValue, fi }, } - if err := survey.Ask(questions, &response); err != nil { + if err := survey.Ask(questions, &response, prompt.Icons); err != nil { return err } @@ -159,6 +167,12 @@ func askManyFlag(cmd *cobra.Command, f *Flag, value interface{}, defaultValue *s return nil } +func askBoolFlag(cmd *cobra.Command, f *Flag, value *bool, defaultValue *bool, isUpdate bool) { + if shouldAsk(cmd, f, isUpdate) { + askBool(cmd, f, value, defaultValue) + } +} + func selectFlag(cmd *cobra.Command, f *Flag, value interface{}, options []string, defaultValue *string, isUpdate bool) error { if shouldAsk(cmd, f, isUpdate) { return _select(cmd, f, value, options, defaultValue, isUpdate) diff --git a/internal/cli/input.go b/internal/cli/input.go index 4bf1e9d8c..20e358d2e 100644 --- a/internal/cli/input.go +++ b/internal/cli/input.go @@ -28,6 +28,11 @@ func ask(cmd *cobra.Command, i commandInput, value interface{}, defaultValue *st return nil } +func askBool(cmd *cobra.Command, i commandInput, value *bool, defaultValue *bool) { + result := prompt.ConfirmDefault(i.GetLabel(), auth0.BoolValue(defaultValue)) + *value = result +} + func _select(cmd *cobra.Command, i commandInput, value interface{}, options []string, defaultValue *string, isUpdate bool) error { isRequired := !isUpdate && i.GetIsRequired() diff --git a/internal/cli/rules.go b/internal/cli/rules.go index 11d0a66b8..bb8aeaa25 100644 --- a/internal/cli/rules.go +++ b/internal/cli/rules.go @@ -32,10 +32,11 @@ var ( } ruleEnabled = Flag{ - Name: "Enabled", - LongForm: "enabled", - ShortForm: "e", - Help: "Enable (or disable) a rule.", + Name: "Enabled", + LongForm: "enabled", + ShortForm: "e", + Help: "Enable (or disable) a rule.", + AlwaysPrompt: true, } ruleScript = Flag{ @@ -301,6 +302,8 @@ auth0 rules update -n "My Updated Rule" --enabled=false`, return err } + ruleEnabled.AskBoolU(cmd, &inputs.Enabled, rule.Enabled) + // TODO(cyx): we can re-think this once we have // `--stdin` based commands. For now we don't have // those yet, so keeping this simple. diff --git a/internal/prompt/prompt.go b/internal/prompt/prompt.go index 7c418d6f2..70850c293 100644 --- a/internal/prompt/prompt.go +++ b/internal/prompt/prompt.go @@ -8,20 +8,20 @@ import ( var stdErrWriter = survey.WithStdio(os.Stdin, os.Stderr, os.Stderr) -var icons = survey.WithIcons(func(icons *survey.IconSet) { +var Icons = survey.WithIcons(func(icons *survey.IconSet) { icons.Question.Text = "" }) func Ask(inputs []*survey.Question, response interface{}) error { - return survey.Ask(inputs, response, stdErrWriter, icons) + return survey.Ask(inputs, response, stdErrWriter, Icons) } func AskOne(input *survey.Question, response interface{}) error { - return survey.Ask([]*survey.Question{input}, response, stdErrWriter, icons) + return survey.Ask([]*survey.Question{input}, response, stdErrWriter, Icons) } func askOne(prompt survey.Prompt, response interface{}) error { - return survey.AskOne(prompt, response, stdErrWriter, icons) + return survey.AskOne(prompt, response, stdErrWriter, Icons) } func TextInput(name string, message string, help string, defaultValue string, required bool) *survey.Question { @@ -37,10 +37,10 @@ func TextInput(name string, message string, help string, defaultValue string, re return input } -func BoolInput(name string, message string, help string, required bool) *survey.Question { +func BoolInput(name string, message string, help string, defaultValue bool, required bool) *survey.Question { input := &survey.Question{ Name: name, - Prompt: &survey.Confirm{Message: message, Help: help}, + Prompt: &survey.Confirm{Message: message, Help: help, Default: defaultValue}, Transform: survey.Title, } @@ -79,3 +79,17 @@ func Confirm(message string) bool { return result } + +func ConfirmDefault(message string, defaultValue bool) bool { + result := false + prompt := &survey.Confirm{ + Message: message, + Default: defaultValue, + } + + if err := askOne(prompt, &result); err != nil { + return defaultValue + } + + return result +}