8000 opts: ListOpts: implement cobra.SliceValue to fix shell completion by thaJeztah · Pull Request #6030 · docker/cli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

opts: ListOpts: implement cobra.SliceValue to fix shell completion #6030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2025
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 8000
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
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
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
0