switch to []int return for multiselect

This commit is contained in:
vilmibm 2023-05-23 00:12:44 -07:00
parent bfdd6e9c4b
commit 5597139df3
4 changed files with 16 additions and 15 deletions

View file

@ -14,7 +14,7 @@ import (
//go:generate moq -rm -out prompter_mock.go . Prompter
type Prompter interface {
Select(string, string, []string) (int, error)
MultiSelect(string, []string, []string) ([]string, error)
MultiSelect(string, []string, []string) ([]int, error)
Input(string, string) (string, error)
InputHostname() (string, error)
Password(string) (string, error)
@ -86,7 +86,7 @@ func (p *surveyPrompter) Select(message, defaultValue string, options []string)
return
}
func (p *surveyPrompter) MultiSelect(message string, defaultValue, options []string) (result []string, err error) {
func (p *surveyPrompter) MultiSelect(message string, defaultValue, options []string) (result []int, err error) {
q := &survey.MultiSelect{
Message: message,
Options: options,
@ -94,8 +94,6 @@ func (p *surveyPrompter) MultiSelect(message string, defaultValue, options []str
Filter: LatinMatchingFilter,
}
// TODO will I regret this returning []string and not []int?
if len(defaultValue) > 0 {
// TODO I don't actually know that this is needed, just being extra cautious
validatedDefault := []string{}

View file

@ -50,7 +50,7 @@ type ConfirmStub struct {
type MultiSelectStub struct {
Prompt string
ExpectedOpts []string
Fn func(string, []string, []string) ([]string, error)
Fn func(string, []string, []string) ([]int, error)
}
type InputHostnameStub struct {
@ -84,8 +84,6 @@ type MockPrompter struct {
ConfirmDeletionStubs []ConfirmDeletionStub
}
// TODO thread safety
func NewMockPrompter(t *testing.T) *MockPrompter {
m := &MockPrompter{
t: t,
@ -120,17 +118,17 @@ func NewMockPrompter(t *testing.T) *MockPrompter {
return s.Fn(p, d, opts)
}
m.MultiSelectFunc = func(p string, d, opts []string) ([]string, error) {
m.MultiSelectFunc = func(p string, d, opts []string) ([]int, error) {
var s MultiSelectStub
if len(m.MultiSelectStubs) > 0 {
s = m.MultiSelectStubs[0]
m.MultiSelectStubs = m.MultiSelectStubs[1:len(m.MultiSelectStubs)]
} else {
return []string{}, NoSuchPromptErr(p)
return []int{}, NoSuchPromptErr(p)
}
if s.Prompt != p {
return []string{}, NoSuchPromptErr(p)
return []int{}, NoSuchPromptErr(p)
}
AssertOptions(m.t, s.ExpectedOpts, opts)
@ -240,7 +238,7 @@ func (m *MockPrompter) RegisterSelect(prompt string, opts []string, stub func(_,
Fn: stub})
}
func (m *MockPrompter) RegisterMultiSelect(prompt string, d, opts []string, stub func(_ string, _, _ []string) ([]string, error)) {
func (m *MockPrompter) RegisterMultiSelect(prompt string, d, opts []string, stub func(_ string, _, _ []string) ([]int, error)) {
m.MultiSelectStubs = append(m.MultiSelectStubs, MultiSelectStub{
Prompt: prompt,
ExpectedOpts: opts,

View file

@ -30,7 +30,7 @@ type platform interface {
Download(url string, dir string) error
}
type iprompter interface {
MultiSelect(string, []string, []string) ([]string, error)
MultiSelect(string, []string, []string) ([]int, error)
}
func NewCmdDownload(f *cmdutil.Factory, runF func(*DownloadOptions) error) *cobra.Command {
@ -131,9 +131,14 @@ func runDownload(opts *DownloadOptions) error {
if len(options) > 10 {
options = options[:10]
}
if wantNames, err = opts.Prompter.MultiSelect("Select artifacts to download:", nil, options); err != nil {
var selected []int
if selected, err = opts.Prompter.MultiSelect("Select artifacts to download:", nil, options); err != nil {
return err
}
wantNames = []string{}
for _, x := range selected {
wantNames = append(wantNames, options[x])
}
if len(wantNames) == 0 {
return errors.New("no artifacts selected")
}

View file

@ -284,8 +284,8 @@ func Test_runDownload(t *testing.T) {
},
promptStubs: func(pm *prompter.MockPrompter) {
pm.RegisterMultiSelect("Select artifacts to download:", nil, []string{"artifact-1", "artifact-2"},
func(_ string, _, opts []string) ([]string, error) {
return []string{"artifact-2"}, nil
func(_ string, _, opts []string) ([]int, error) {
return []int{1}, nil
})
},
},