From 5597139df3c9aeedcf8b6ac3379bb59dda4a1778 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Tue, 23 May 2023 00:12:44 -0700 Subject: [PATCH] switch to []int return for multiselect --- internal/prompter/prompter.go | 6 ++---- internal/prompter/test.go | 12 +++++------- pkg/cmd/run/download/download.go | 9 +++++++-- pkg/cmd/run/download/download_test.go | 4 ++-- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/internal/prompter/prompter.go b/internal/prompter/prompter.go index 9803f8a49..b91445c49 100644 --- a/internal/prompter/prompter.go +++ b/internal/prompter/prompter.go @@ -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{} diff --git a/internal/prompter/test.go b/internal/prompter/test.go index a5cebacda..c376e9c83 100644 --- a/internal/prompter/test.go +++ b/internal/prompter/test.go @@ -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, diff --git a/pkg/cmd/run/download/download.go b/pkg/cmd/run/download/download.go index 4c5322806..993e16e52 100644 --- a/pkg/cmd/run/download/download.go +++ b/pkg/cmd/run/download/download.go @@ -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") } diff --git a/pkg/cmd/run/download/download_test.go b/pkg/cmd/run/download/download_test.go index 3640f93a0..3c1c8f2d8 100644 --- a/pkg/cmd/run/download/download_test.go +++ b/pkg/cmd/run/download/download_test.go @@ -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 }) }, },