mirror of https://github.com/docker/docs.git
Merge pull request #20480 from wenchma/20431-filter_since_before
Enhancement of docker ps before and since filters
This commit is contained in:
commit
9eb4575f8c
|
@ -226,8 +226,24 @@ func (daemon *Daemon) foldFilter(config *types.ContainerListOptions) (*listConte
|
||||||
// includeContainerInList decides whether a containers should be include in the output or not based in the filter.
|
// includeContainerInList decides whether a containers should be include in the output or not based in the filter.
|
||||||
// It also decides if the iteration should be stopped or not.
|
// It also decides if the iteration should be stopped or not.
|
||||||
func includeContainerInList(container *container.Container, ctx *listContext) iterationAction {
|
func includeContainerInList(container *container.Container, ctx *listContext) iterationAction {
|
||||||
|
// Do not include container if it's in the list before the filter container.
|
||||||
|
// Set the filter container to nil to include the rest of containers after this one.
|
||||||
|
if ctx.beforeFilter != nil {
|
||||||
|
if container.ID == ctx.beforeFilter.ID {
|
||||||
|
ctx.beforeFilter = nil
|
||||||
|
}
|
||||||
|
return excludeContainer
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stop iteration when the container arrives to the filter container
|
||||||
|
if ctx.sinceFilter != nil {
|
||||||
|
if container.ID == ctx.sinceFilter.ID {
|
||||||
|
return stopIteration
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Do not include container if it's stopped and we're not filters
|
// Do not include container if it's stopped and we're not filters
|
||||||
// FIXME remove the ctx.beforContainer part of the condition for 1.12 as --since and --before are deprecated
|
// FIXME remove the ctx.beforContainer and ctx.sinceContainer part of the condition for 1.12 as --since and --before are deprecated
|
||||||
if !container.Running && !ctx.All && ctx.Limit <= 0 && ctx.beforeContainer == nil && ctx.sinceContainer == nil {
|
if !container.Running && !ctx.All && ctx.Limit <= 0 && ctx.beforeContainer == nil && ctx.sinceContainer == nil {
|
||||||
return excludeContainer
|
return excludeContainer
|
||||||
}
|
}
|
||||||
|
@ -267,22 +283,6 @@ func includeContainerInList(container *container.Container, ctx *listContext) it
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do not include container if it's in the list before the filter container.
|
|
||||||
// Set the filter container to nil to include the rest of containers after this one.
|
|
||||||
if ctx.beforeFilter != nil {
|
|
||||||
if container.ID == ctx.beforeFilter.ID {
|
|
||||||
ctx.beforeFilter = nil
|
|
||||||
}
|
|
||||||
return excludeContainer
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stop iteration when the container arrives to the filter container
|
|
||||||
if ctx.sinceFilter != nil {
|
|
||||||
if container.ID == ctx.sinceFilter.ID {
|
|
||||||
return stopIteration
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stop iteration when the index is over the limit
|
// Stop iteration when the index is over the limit
|
||||||
if ctx.Limit > 0 && ctx.idx == ctx.Limit {
|
if ctx.Limit > 0 && ctx.idx == ctx.Limit {
|
||||||
return stopIteration
|
return stopIteration
|
||||||
|
|
|
@ -64,6 +64,10 @@ func (s *DockerSuite) TestPsListContainersBase(c *check.C) {
|
||||||
expected = []string{fourthID, secondID}
|
expected = []string{fourthID, secondID}
|
||||||
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter: Container list is not in the correct order: \n%s", out))
|
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter: Container list is not in the correct order: \n%s", out))
|
||||||
|
|
||||||
|
out, _ = dockerCmd(c, "ps", "-f", "since="+thirdID)
|
||||||
|
expected = []string{fourthID}
|
||||||
|
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter: Container list is not in the correct order: \n%s", out))
|
||||||
|
|
||||||
// filter before
|
// filter before
|
||||||
out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID, "-a")
|
out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID, "-a")
|
||||||
expected = []string{thirdID, secondID, firstID}
|
expected = []string{thirdID, secondID, firstID}
|
||||||
|
@ -73,6 +77,10 @@ func (s *DockerSuite) TestPsListContainersBase(c *check.C) {
|
||||||
expected = []string{secondID, firstID}
|
expected = []string{secondID, firstID}
|
||||||
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE filter: Container list is not in the correct order: \n%s", out))
|
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE filter: Container list is not in the correct order: \n%s", out))
|
||||||
|
|
||||||
|
out, _ = dockerCmd(c, "ps", "-f", "before="+thirdID)
|
||||||
|
expected = []string{secondID, firstID}
|
||||||
|
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter: Container list is not in the correct order: \n%s", out))
|
||||||
|
|
||||||
// filter since & before
|
// filter since & before
|
||||||
out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID, "-a")
|
out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID, "-a")
|
||||||
expected = []string{thirdID, secondID}
|
expected = []string{thirdID, secondID}
|
||||||
|
|
Loading…
Reference in New Issue