Explorar o código

Implement docker ps with standanlone client lib.

Signed-off-by: David Calavera <david.calavera@gmail.com>
David Calavera %!s(int64=9) %!d(string=hai) anos
pai
achega
d05aa418b0
Modificáronse 2 ficheiros con 75 adicións e 41 borrados
  1. 66 0
      api/client/lib/container_list.go
  2. 9 41
      api/client/ps.go

+ 66 - 0
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
+}

+ 9 - 41
api/client/ps.go

@@ -1,12 +1,8 @@
 package client
 package client
 
 
 import (
 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/client/ps"
-	"github.com/docker/docker/api/types"
 	Cli "github.com/docker/docker/cli"
 	Cli "github.com/docker/docker/cli"
 	"github.com/docker/docker/opts"
 	"github.com/docker/docker/opts"
 	flag "github.com/docker/docker/pkg/mflag"
 	flag "github.com/docker/docker/pkg/mflag"
@@ -21,7 +17,6 @@ func (cli *DockerCli) CmdPs(args ...string) error {
 		err error
 		err error
 
 
 		psFilterArgs = filters.NewArgs()
 		psFilterArgs = filters.NewArgs()
-		v            = url.Values{}
 
 
 		cmd      = Cli.Subcmd("ps", nil, Cli.DockerCommands["ps"].Description, true)
 		cmd      = Cli.Subcmd("ps", nil, Cli.DockerCommands["ps"].Description, true)
 		quiet    = cmd.Bool([]string{"q", "-quiet"}, false, "Only display numeric IDs")
 		quiet    = cmd.Bool([]string{"q", "-quiet"}, false, "Only display numeric IDs")
@@ -44,26 +39,6 @@ func (cli *DockerCli) CmdPs(args ...string) error {
 		*last = 1
 		*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.
 	// Consolidate all filter flags, and sanity check them.
 	// They'll get processed in the daemon/server.
 	// They'll get processed in the daemon/server.
 	for _, f := range flFilter.GetAll() {
 	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 {
 	if err != nil {
 		return err
 		return err
 	}
 	}
 
 
-	defer serverResp.body.Close()
-
-	containers := []types.Container{}
-	if err := json.NewDecoder(serverResp.body).Decode(&containers); err != nil {
-		return err
-	}
-
 	f := *format
 	f := *format
 	if len(f) == 0 {
 	if len(f) == 0 {
 		if len(cli.PsFormat()) > 0 && !*quiet {
 		if len(cli.PsFormat()) > 0 && !*quiet {