Skip to content
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

Fix handling of multiple filters in podman ps #1345

Closed
wants to merge 2 commits into from
Closed
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
9 changes: 4 additions & 5 deletions cmd/podman/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ var (
Name: "all, a",
Usage: "Show all the containers, default is only running containers",
},
cli.StringFlag{
cli.StringSliceFlag{
Name: "filter, f",
Usage: "Filter output based on conditions given",
},
Expand Down Expand Up @@ -222,7 +222,6 @@ func psCmd(c *cli.Context) error {

opts := shared.PsOptions{
All: c.Bool("all"),
Filter: c.String("filter"),
Format: format,
Last: c.Int("last"),
Latest: c.Bool("latest"),
Expand All @@ -246,8 +245,8 @@ func psCmd(c *cli.Context) error {
})
}

if opts.Filter != "" {
filters := strings.Split(opts.Filter, ",")
filters := c.StringSlice("filter")
if len(filters) > 0 {
for _, f := range filters {
filterSplit := strings.SplitN(f, "=", 2)
if len(filterSplit) < 2 {
Expand Down Expand Up @@ -317,7 +316,7 @@ func generateContainerFilterFuncs(filter, filterValue string, runtime *libpod.Ru
return strings.Contains(c.ID(), filterValue)
}, nil
case "label":
var filterArray []string = strings.Split(filterValue, "=")
var filterArray []string = strings.SplitN(filterValue, "=", 2)
var filterKey string = filterArray[0]
if len(filterArray) > 1 {
filterValue = filterArray[1]
Expand Down
1 change: 0 additions & 1 deletion cmd/podman/shared/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
// PsOptions describes the struct being formed for ps
type PsOptions struct {
All bool
Filter string
Format string
Last int
Latest bool
Expand Down
4 changes: 3 additions & 1 deletion docs/podman-ps.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ Display namespace information

**--filter, -f**

Filter output based on conditions given
Filter what containers are shown in the output.
Multiple filters can be given with multiple uses of the --filter flag.
If multiple filters are given, only containers which match all of the given filters will be shown.

Valid filters are listed below:

Expand Down
19 changes: 19 additions & 0 deletions test/e2e/ps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,25 @@ var _ = Describe("Podman ps", func() {
Expect(result.OutputToStringArray()[0]).To(Equal(fullCid))
})

It("podman ps multiple filters", func() {
session := podmanTest.Podman([]string{"run", "-d", "--name", "test1", "--label", "key1=value1", ALPINE, "top"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
fullCid := session.OutputToString()

session2 := podmanTest.Podman([]string{"run", "-d", "--name", "test2", "--label", "key1=value1", ALPINE, "top"})
session2.WaitWithDefaultTimeout()
Expect(session2.ExitCode()).To(Equal(0))

result := podmanTest.Podman([]string{"ps", "-aq", "--no-trunc", "--filter", "name=test1", "--filter", "label=key1=value1"})
result.WaitWithDefaultTimeout()
Expect(result.ExitCode()).To(Equal(0))

output := result.OutputToStringArray()
Expect(len(output)).To(Equal(1))
Expect(output[0]).To(Equal(fullCid))
})

It("podman ps mutually exclusive flags", func() {
session := podmanTest.Podman([]string{"ps", "-aqs"})
session.WaitWithDefaultTimeout()
Expand Down