Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions opts/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ func (opts *ListOpts) GetAll() []string {
return *opts.values
}

// GetSlice returns the values of slice.
//
// It implements [cobra.SliceValue] to allow shell completion to be provided
// multiple times.
//
// [cobra.SliceValue]: https://pkg.go.dev/github.com/spf13/cobra@v1.9.1#SliceValue
func (opts *ListOpts) GetSlice() []string {
return *opts.values
}

// GetAllOrEmpty returns the values of the slice
// or an empty slice when there are no values.
func (opts *ListOpts) GetAllOrEmpty() []string {
Expand Down
10 changes: 6 additions & 4 deletions opts/opts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func TestMapOpts(t *testing.T) {
}
}

//nolint:gocyclo // ignore "cyclomatic complexity 17 is too high"
func TestListOptsWithoutValidator(t *testing.T) {
o := NewListOpts(nil)
err := o.Set("foo")
Expand Down Expand Up @@ -145,12 +146,13 @@ func TestListOptsWithoutValidator(t *testing.T) {
if o.String() != "[bar bar]" {
t.Errorf("%s != [bar bar]", o.String())
}
listOpts := o.GetAll()
if len(listOpts) != 2 || listOpts[0] != "bar" || listOpts[1] != "bar" {
if listOpts := o.GetAll(); len(listOpts) != 2 || listOpts[0] != "bar" || listOpts[1] != "bar" {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A DeepEqual might make this easier to read, and avoid the nolint? The is is already imported anyway. I've not tried it, but something like ...

assert.Check(t, is.DeepEqual(o.GetAll(), []string{"bar", "bar"}))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, I want to have a look at this file, as there's more to fix in that area, so for now I chose to "mostly" keep it as-is.

Will likely do some follow-ups to modernise tests in this package a bit.

t.Errorf("Expected [[bar bar]], got [%v]", listOpts)
}
mapListOpts := o.GetMap()
if len(mapListOpts) != 1 {
if listOpts := o.GetSlice(); len(listOpts) != 2 || listOpts[0] != "bar" || listOpts[1] != "bar" {
t.Errorf("Expected [[bar bar]], got [%v]", listOpts)
}
if mapListOpts := o.GetMap(); len(mapListOpts) != 1 {
t.Errorf("Expected [map[bar:{}]], got [%v]", mapListOpts)
}
}
Expand Down
Loading