mirror of https://github.com/docker/docs.git
Implement docker ps with standanlone client lib.
Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
parent
eeee2eae86
commit
d05aa418b0
|
@ -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
|
||||||
|
}
|
|
@ -1,12 +1,8 @@
|
||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"github.com/docker/docker/api/client/lib"
|
||||||
"net/url"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"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 {
|
options := lib.ContainerListOptions{
|
||||||
filterJSON, err := filters.ToParam(psFilterArgs)
|
All: *all,
|
||||||
if err != nil {
|
Limit: *last,
|
||||||
return err
|
Since: *since,
|
||||||
}
|
Before: *before,
|
||||||
|
Size: *size,
|
||||||
v.Set("filters", filterJSON)
|
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 {
|
||||||
|
|
Loading…
Reference in New Issue