Skip to content

Commit

Permalink
Tweak flags so we don't repeat ourselves too much
Browse files Browse the repository at this point in the history
  • Loading branch information
cyx committed Mar 20, 2021
1 parent 313db86 commit ddee885
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
6 changes: 6 additions & 0 deletions internal/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ type Flag struct {
IsRequired bool
}

func (f *Flag) Required() *Flag {
clone := *f
clone.IsRequired = true
return &clone
}

func (f *Flag) Ask(cmd *cobra.Command, value interface{}) error {
return askInput(cmd, f, value, false)
}
Expand Down
18 changes: 18 additions & 0 deletions internal/cli/flags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cli

import "testing"

func TestFlagsRequiredClone(t *testing.T) {
orig := &Flag{
Name: "some-flag",
}

clone := orig.Required()
if !clone.IsRequired {
t.Fatal("wanted flag to be required")
}

if orig.IsRequired {
t.Fatal("wanted original to be left intact")
}
}
12 changes: 2 additions & 10 deletions internal/cli/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ import (
)

var (
ruleNameRequired = Flag{
Name: "Name",
LongForm: "name",
ShortForm: "n",
Help: "Name of the rule.",
IsRequired: true,
}

ruleName = Flag{
Name: "Name",
LongForm: "name",
Expand Down Expand Up @@ -113,7 +105,7 @@ auth0 rules create --name "My Rule" --template [empty-rule]"
prepareInteractivity(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
if err := ruleNameRequired.Ask(cmd, &inputs.Name); err != nil {
if err := ruleName.Required().Ask(cmd, &inputs.Name); err != nil {
return err
}

Expand Down Expand Up @@ -151,7 +143,7 @@ auth0 rules create --name "My Rule" --template [empty-rule]"
},
}

ruleNameRequired.RegisterString(cmd, &inputs.Name, "")
ruleName.Required().RegisterString(cmd, &inputs.Name, "")
ruleTemplate.RegisterString(cmd, &inputs.Template, "")
ruleEnabled.RegisterBool(cmd, &inputs.Enabled, true)

Expand Down

0 comments on commit ddee885

Please sign in to comment.