Use opts.FilterOpt for filter flags.
Signed-off-by: Daniel Nephin <dnephin@docker.com> Upstream-commit: a07be9be38a96f43a0a7bac48caf35f9370bb543 Component: engine
This commit is contained in:
@ -1,16 +1,15 @@
|
||||
package container
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"github.com/docker/docker/cli"
|
||||
"github.com/docker/docker/cli/command"
|
||||
"github.com/docker/docker/cli/command/formatter"
|
||||
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/docker/docker/opts"
|
||||
"github.com/docker/docker/utils/templates"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
@ -23,12 +22,12 @@ type psOptions struct {
|
||||
nLatest bool
|
||||
last int
|
||||
format string
|
||||
filter []string
|
||||
filter opts.FilterOpt
|
||||
}
|
||||
|
||||
// NewPsCommand creates a new cobra.Command for `docker ps`
|
||||
func NewPsCommand(dockerCli *command.DockerCli) *cobra.Command {
|
||||
var opts psOptions
|
||||
opts := psOptions{filter: opts.NewFilterOpt()}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "ps [OPTIONS]",
|
||||
@ -48,7 +47,7 @@ func NewPsCommand(dockerCli *command.DockerCli) *cobra.Command {
|
||||
flags.BoolVarP(&opts.nLatest, "latest", "l", false, "Show the latest created container (includes all states)")
|
||||
flags.IntVarP(&opts.last, "last", "n", -1, "Show n last created containers (includes all states)")
|
||||
flags.StringVarP(&opts.format, "format", "", "", "Pretty-print containers using a Go template")
|
||||
flags.StringSliceVarP(&opts.filter, "filter", "f", []string{}, "Filter output based on conditions provided")
|
||||
flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided")
|
||||
|
||||
return cmd
|
||||
}
|
||||
@ -65,26 +64,17 @@ func (p *preProcessor) Size() bool {
|
||||
}
|
||||
|
||||
func buildContainerListOptions(opts *psOptions) (*types.ContainerListOptions, error) {
|
||||
|
||||
options := &types.ContainerListOptions{
|
||||
All: opts.all,
|
||||
Limit: opts.last,
|
||||
Size: opts.size,
|
||||
Filter: filters.NewArgs(),
|
||||
Filter: opts.filter.Value(),
|
||||
}
|
||||
|
||||
if opts.nLatest && opts.last == -1 {
|
||||
options.Limit = 1
|
||||
}
|
||||
|
||||
for _, f := range opts.filter {
|
||||
var err error
|
||||
options.Filter, err = filters.ParseFlag(f, options.Filter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// Currently only used with Size, so we can determine if the user
|
||||
// put {{.Size}} in their format.
|
||||
pre := &preProcessor{opts: options}
|
||||
|
||||
@ -1,8 +1,16 @@
|
||||
package container
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/opts"
|
||||
"github.com/docker/docker/pkg/testutil/assert"
|
||||
)
|
||||
|
||||
func TestBuildContainerListOptions(t *testing.T) {
|
||||
filters := opts.NewFilterOpt()
|
||||
assert.NilError(t, filters.Set("foo=bar"))
|
||||
assert.NilError(t, filters.Set("baz=foo"))
|
||||
|
||||
contexts := []struct {
|
||||
psOpts *psOptions
|
||||
@ -16,7 +24,7 @@ func TestBuildContainerListOptions(t *testing.T) {
|
||||
all: true,
|
||||
size: true,
|
||||
last: 5,
|
||||
filter: []string{"foo=bar", "baz=foo"},
|
||||
filter: filters,
|
||||
},
|
||||
expectedAll: true,
|
||||
expectedSize: true,
|
||||
@ -42,27 +50,12 @@ func TestBuildContainerListOptions(t *testing.T) {
|
||||
|
||||
for _, c := range contexts {
|
||||
options, err := buildContainerListOptions(c.psOpts)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.NilError(t, err)
|
||||
|
||||
if c.expectedAll != options.All {
|
||||
t.Fatalf("Expected All to be %t but got %t", c.expectedAll, options.All)
|
||||
}
|
||||
|
||||
if c.expectedSize != options.Size {
|
||||
t.Fatalf("Expected Size to be %t but got %t", c.expectedSize, options.Size)
|
||||
}
|
||||
|
||||
if c.expectedLimit != options.Limit {
|
||||
t.Fatalf("Expected Limit to be %d but got %d", c.expectedLimit, options.Limit)
|
||||
}
|
||||
|
||||
f := options.Filter
|
||||
|
||||
if f.Len() != len(c.expectedFilters) {
|
||||
t.Fatalf("Expected %d filters but got %d", len(c.expectedFilters), f.Len())
|
||||
}
|
||||
assert.Equal(t, c.expectedAll, options.All)
|
||||
assert.Equal(t, c.expectedSize, options.Size)
|
||||
assert.Equal(t, c.expectedLimit, options.Limit)
|
||||
assert.Equal(t, options.Filter.Len(), len(c.expectedFilters))
|
||||
|
||||
for k, v := range c.expectedFilters {
|
||||
f := options.Filter
|
||||
|
||||
Reference in New Issue
Block a user