Implement docker top with standalone client lib.

Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
David Calavera 2015-12-04 13:52:40 -05:00
parent 21ffdf0e0e
commit 7573c153c5
2 changed files with 32 additions and 13 deletions

View File

@ -0,0 +1,29 @@
package lib
import (
"encoding/json"
"net/url"
"strings"
"github.com/docker/docker/api/types"
)
// ContainerTop shows process information from within a container.
func (cli *Client) ContainerTop(containerID string, arguments []string) (types.ContainerProcessList, error) {
var (
query url.Values
response types.ContainerProcessList
)
if len(arguments) > 0 {
query.Set("ps_args", strings.Join(arguments, " "))
}
resp, err := cli.get("/containers/"+containerID+"/top", query, nil)
if err != nil {
return response, err
}
defer ensureReaderClosed(resp)
err = json.NewDecoder(resp.body).Decode(&response)
return response, err
}

View File

@ -1,13 +1,10 @@
package client
import (
"encoding/json"
"fmt"
"net/url"
"strings"
"text/tabwriter"
"github.com/docker/docker/api/types"
Cli "github.com/docker/docker/cli"
flag "github.com/docker/docker/pkg/mflag"
)
@ -21,23 +18,16 @@ func (cli *DockerCli) CmdTop(args ...string) error {
cmd.ParseFlags(args, true)
val := url.Values{}
var arguments []string
if cmd.NArg() > 1 {
val.Set("ps_args", strings.Join(cmd.Args()[1:], " "))
arguments = cmd.Args()[1:]
}
serverResp, err := cli.call("GET", "/containers/"+cmd.Arg(0)+"/top?"+val.Encode(), nil, nil)
procList, err := cli.client.ContainerTop(cmd.Arg(0), arguments)
if err != nil {
return err
}
defer serverResp.body.Close()
procList := types.ContainerProcessList{}
if err := json.NewDecoder(serverResp.body).Decode(&procList); err != nil {
return err
}
w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
fmt.Fprintln(w, strings.Join(procList.Titles, "\t"))