Skip to content

Commit

Permalink
Refactor selectFlag to selectInput and use commandInput
Browse files Browse the repository at this point in the history
  • Loading branch information
cyx committed Mar 23, 2021
1 parent bb424bf commit 4ef31b2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
14 changes: 11 additions & 3 deletions internal/cli/arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/spf13/cobra"
)

var _ commandInput = Argument{}

type Argument struct {
Name string
Help string
Expand All @@ -20,6 +22,10 @@ func (a Argument) GetLabel() string {
return inputLabel(a.Name)
}

func (a Argument) GetLongForm() string {
return a.Name
}

func (a Argument) GetHelp() string {
return a.Help
}
Expand All @@ -28,6 +34,10 @@ func (a Argument) GetIsRequired() bool {
return true
}

func (a Argument) GetAlwaysPrompt() bool {
return false
}

func (a *Argument) Ask(cmd *cobra.Command, value interface{}) error {
return askArgument(cmd, a, value)
}
Expand All @@ -46,11 +56,9 @@ func (a *Argument) Picker(cmd *cobra.Command, result *string, fn pickerOptionsFu
return err
}

// TODO(cyx): Fix this up. For now everything depends on `flag`.
f := Flag{Name: a.Name}
defaultLabel := opts.defaultLabel()
var val string
if err := selectFlag(cmd, &f, &val, opts.labels(), &defaultLabel, false); err != nil {
if err := selectInput(cmd, a, &val, opts.labels(), &defaultLabel, false); err != nil {
return err
}

Expand Down
28 changes: 19 additions & 9 deletions internal/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/spf13/cobra"
)

var _ commandInput = Flag{}

type Flag struct {
Name string
LongForm string
Expand All @@ -23,6 +25,10 @@ func (f Flag) GetLabel() string {
return inputLabel(f.Name)
}

func (f Flag) GetLongForm() string {
return f.LongForm
}

func (f Flag) GetHelp() string {
return f.Help
}
Expand All @@ -31,6 +37,10 @@ func (f Flag) GetIsRequired() bool {
return f.IsRequired
}

func (f Flag) GetAlwaysPrompt() bool {
return f.AlwaysPrompt
}

func (f *Flag) Ask(cmd *cobra.Command, value interface{}, defaultValue *string) error {
return askFlag(cmd, f, value, defaultValue, false)
}
Expand All @@ -48,11 +58,11 @@ func (f *Flag) AskManyU(cmd *cobra.Command, value interface{}, defaultValue *str
}

func (f *Flag) Select(cmd *cobra.Command, value interface{}, options []string, defaultValue *string) error {
return selectFlag(cmd, f, value, options, defaultValue, false)
return selectInput(cmd, f, value, options, defaultValue, false)
}

func (f *Flag) SelectU(cmd *cobra.Command, value interface{}, options []string, defaultValue *string) error {
return selectFlag(cmd, f, value, options, defaultValue, true)
return selectInput(cmd, f, value, options, defaultValue, true)
}

func (f *Flag) RegisterString(cmd *cobra.Command, value *string, defaultValue string) {
Expand Down Expand Up @@ -107,9 +117,9 @@ func askManyFlag(cmd *cobra.Command, f *Flag, value interface{}, defaultValue *s
return nil
}

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)
func selectInput(cmd *cobra.Command, i commandInput, value interface{}, options []string, defaultValue *string, isUpdate bool) error {
if shouldAsk(cmd, i, isUpdate) {
return _select(cmd, i, value, options, defaultValue, isUpdate)
}

return nil
Expand Down Expand Up @@ -147,16 +157,16 @@ func registerBool(cmd *cobra.Command, f *Flag, value *bool, defaultValue bool, i
}
}

func shouldAsk(cmd *cobra.Command, f *Flag, isUpdate bool) bool {
func shouldAsk(cmd *cobra.Command, i commandInput, isUpdate bool) bool {
if isUpdate {
if !f.IsRequired && !f.AlwaysPrompt {
if !i.GetIsRequired() && !i.GetAlwaysPrompt() {
return false
}

return shouldPromptWhenFlagless(cmd, f.LongForm)
return shouldPromptWhenFlagless(cmd, i.GetLongForm())
}

return shouldPrompt(cmd, f.LongForm)
return shouldPrompt(cmd, i.GetLongForm())
}

func markFlagRequired(cmd *cobra.Command, f *Flag, isUpdate bool) error {
Expand Down
2 changes: 2 additions & 0 deletions internal/cli/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ type commandInput interface {
GetName() string
GetLabel() string
GetHelp() string
GetLongForm() string
GetIsRequired() bool
GetAlwaysPrompt() bool
}

func ask(cmd *cobra.Command, i commandInput, value interface{}, defaultValue *string, isUpdate bool) error {
Expand Down

0 comments on commit 4ef31b2

Please sign in to comment.