From d05aa418b0466553a24d42896f99176cfa29765f Mon Sep 17 00:00:00 2001 From: David Calavera Date: Fri, 4 Dec 2015 11:59:44 -0500 Subject: [PATCH] Implement docker ps with standanlone client lib. Signed-off-by: David Calavera --- api/client/lib/container_list.go | 66 ++++++++++++++++++++++++++++++++ api/client/ps.go | 50 +++++------------------- 2 files changed, 75 insertions(+), 41 deletions(-) create mode 100644 api/client/lib/container_list.go diff --git a/api/client/lib/container_list.go b/api/client/lib/container_list.go new file mode 100644 index 0000000000..51451f6618 --- /dev/null +++ b/api/client/lib/container_list.go @@ -0,0 +1,66 @@ +package lib + +import ( + "encoding/json" + "net/url" + "strconv" + + "github.com/docker/docker/api/types" + "github.com/docker/docker/pkg/parsers/filters" +) + +// ContainerListOptions holds parameters to list containers with. +type ContainerListOptions struct { + Quiet bool + Size bool + All bool + Latest bool + Since string + Before string + Limit int + Filter filters.Args +} + +// ContainerList returns the list of containers in the docker host. +func (cli *Client) ContainerList(options ContainerListOptions) ([]types.Container, error) { + var query url.Values + + if options.All { + query.Set("all", "1") + } + + if options.Limit != -1 { + query.Set("limit", strconv.Itoa(options.Limit)) + } + + if options.Since != "" { + query.Set("since", options.Since) + } + + if options.Before != "" { + query.Set("before", options.Before) + } + + if options.Size { + query.Set("size", "1") + } + + if options.Filter.Len() > 0 { + filterJSON, err := filters.ToParam(options.Filter) + if err != nil { + return nil, err + } + + query.Set("filters", filterJSON) + } + + resp, err := cli.GET("/containers/json", query, nil) + if err != nil { + return nil, err + } + defer ensureReaderClosed(resp) + + var containers []types.Container + err = json.NewDecoder(resp.body).Decode(&containers) + return containers, err +} diff --git a/api/client/ps.go b/api/client/ps.go index 5bcf4bb2ee..7810cfeaaf 100644 --- a/api/client/ps.go +++ b/api/client/ps.go @@ -1,12 +1,8 @@ package client import ( - "encoding/json" - "net/url" - "strconv" - + "github.com/docker/docker/api/client/lib" "github.com/docker/docker/api/client/ps" - "github.com/docker/docker/api/types" Cli "github.com/docker/docker/cli" "github.com/docker/docker/opts" flag "github.com/docker/docker/pkg/mflag" @@ -21,7 +17,6 @@ func (cli *DockerCli) CmdPs(args ...string) error { err error psFilterArgs = filters.NewArgs() - v = url.Values{} cmd = Cli.Subcmd("ps", nil, Cli.DockerCommands["ps"].Description, true) quiet = cmd.Bool([]string{"q", "-quiet"}, false, "Only display numeric IDs") @@ -44,26 +39,6 @@ func (cli *DockerCli) CmdPs(args ...string) error { *last = 1 } - if *all { - v.Set("all", "1") - } - - if *last != -1 { - v.Set("limit", strconv.Itoa(*last)) - } - - if *since != "" { - v.Set("since", *since) - } - - if *before != "" { - v.Set("before", *before) - } - - if *size { - v.Set("size", "1") - } - // Consolidate all filter flags, and sanity check them. // They'll get processed in the daemon/server. for _, f := range flFilter.GetAll() { @@ -72,27 +47,20 @@ func (cli *DockerCli) CmdPs(args ...string) error { } } - if psFilterArgs.Len() > 0 { - filterJSON, err := filters.ToParam(psFilterArgs) - if err != nil { - return err - } - - v.Set("filters", filterJSON) + options := lib.ContainerListOptions{ + All: *all, + Limit: *last, + Since: *since, + Before: *before, + Size: *size, + Filter: psFilterArgs, } - serverResp, err := cli.call("GET", "/containers/json?"+v.Encode(), nil, nil) + containers, err := cli.client.ContainerList(options) if err != nil { return err } - defer serverResp.body.Close() - - containers := []types.Container{} - if err := json.NewDecoder(serverResp.body).Decode(&containers); err != nil { - return err - } - f := *format if len(f) == 0 { if len(cli.PsFormat()) > 0 && !*quiet {