Skip to content

Commit

Permalink
Ask for rule enable/disable on 'rules update' (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
Widcket authored Apr 3, 2021
1 parent d13ce0d commit 804d3b6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
16 changes: 15 additions & 1 deletion internal/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions internal/cli/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
11 changes: 7 additions & 4 deletions internal/cli/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -301,6 +302,8 @@ auth0 rules update <rule-id> -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.
Expand Down
26 changes: 20 additions & 6 deletions internal/prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
}

Expand Down Expand Up @@ -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
}

0 comments on commit 804d3b6

Please sign in to comment.