diff --git a/api.go b/api.go index 136cd52d7c..6d14f1f17b 100644 --- a/api.go +++ b/api.go @@ -186,12 +186,14 @@ func getContainers(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, all := r.Form.Get("all") == "1" trunc_cmd := r.Form.Get("trunc_cmd") != "0" only_ids := r.Form.Get("only_ids") == "1" + since := r.Form.Get("since") + before := r.Form.Get("before") n, err := strconv.Atoi(r.Form.Get("limit")) if err != nil { n = -1 } - outs := srv.Containers(all, trunc_cmd, only_ids, n) + outs := srv.Containers(all, trunc_cmd, only_ids, n, since, before) b, err := json.Marshal(outs) if err != nil { return nil, err diff --git a/commands.go b/commands.go index 02e02d6cec..81575c7b14 100644 --- a/commands.go +++ b/commands.go @@ -766,6 +766,8 @@ func CmdPs(args ...string) error { all := cmd.Bool("a", false, "Show all containers. Only running containers are shown by default.") noTrunc := cmd.Bool("notrunc", false, "Don't truncate output") nLatest := cmd.Bool("l", false, "Show only the latest created container, include non-running ones.") + since := cmd.String("sinceId", "", "Show only containers created since Id, include non-running ones.") + before := cmd.String("beforeId", "", "Show only container created before Id, include non-running ones.") last := cmd.Int("n", -1, "Show n last created containers, include non-running ones.") if err := cmd.Parse(args); err != nil { @@ -787,6 +789,12 @@ func CmdPs(args ...string) error { if *last != -1 { v.Set("limit", strconv.Itoa(*last)) } + if *since != "" { + v.Set("since", *since) + } + if *before != "" { + v.Set("before", *before) + } body, _, err := call("GET", "/containers?"+v.Encode(), nil) if err != nil { diff --git a/docs/sources/remote-api/api.rst b/docs/sources/remote-api/api.rst index 25007af123..eb26cf97ad 100644 --- a/docs/sources/remote-api/api.rst +++ b/docs/sources/remote-api/api.rst @@ -28,7 +28,7 @@ List containers .. sourcecode:: http - GET /containers?trunc_cmd=0&all=1&only_ids=0 HTTP/1.1 + GET /containers?trunc_cmd=0&all=1&only_ids=0&before=8dfafdbc3a40 HTTP/1.1 **Example response**: @@ -71,6 +71,8 @@ List containers :query all: 1 or 0, Show all containers. Only running containers are shown by default :query trunc_cmd: 1 or 0, Truncate output. Output is truncated by default :query limit: Show ``limit`` last created containers, include non-running ones. + :query since: Show only containers created since Id, include non-running ones. + :query before: Show only containers created before Id, include non-running ones. :statuscode 200: no error :statuscode 500: server error diff --git a/server.go b/server.go index f57d8e6f06..ffc66d622a 100644 --- a/server.go +++ b/server.go @@ -235,15 +235,30 @@ func (srv *Server) ContainerChanges(name string) ([]Change, error) { return nil, fmt.Errorf("No such container: %s", name) } -func (srv *Server) Containers(all, trunc_cmd, only_ids bool, n int) []ApiContainers { +func (srv *Server) Containers(all, trunc_cmd, only_ids bool, n int, since, before string) []ApiContainers { var outs []ApiContainers = []ApiContainers{} //produce [] when empty instead of 'null' - for i, container := range srv.runtime.List() { - if !container.State.Running && !all && n == -1 { + var foundBefore bool + var displayed int + for _, container := range srv.runtime.List() { + if !container.State.Running && !all && n == -1 && since == "" && before == "" { continue } - if i == n { + if before != "" { + if container.ShortId() == before { + foundBefore = true + continue + } + if !foundBefore { + continue + } + } + if displayed == n { break } + if container.ShortId() == since { + break + } + displayed += 1 var out ApiContainers out.Id = container.ShortId() if !only_ids {