diff --git a/internal/cli/arguments.go b/internal/cli/arguments.go index f86598947..9c5382792 100644 --- a/internal/cli/arguments.go +++ b/internal/cli/arguments.go @@ -7,6 +7,8 @@ import ( "github.com/spf13/cobra" ) +var _ commandInput = Argument{} + type Argument struct { Name string Help string @@ -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 } @@ -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) } @@ -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 } diff --git a/internal/cli/flags.go b/internal/cli/flags.go index 3941b93c6..7fa1bfc7b 100644 --- a/internal/cli/flags.go +++ b/internal/cli/flags.go @@ -6,6 +6,8 @@ import ( "github.com/spf13/cobra" ) +var _ commandInput = Flag{} + type Flag struct { Name string LongForm string @@ -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 } @@ -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) } @@ -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) { @@ -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 @@ -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 { diff --git a/internal/cli/input.go b/internal/cli/input.go index 7111bbbe1..6853d639c 100644 --- a/internal/cli/input.go +++ b/internal/cli/input.go @@ -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 {