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 package client
import ( import (
"encoding/json"
"fmt" "fmt"
"net/url"
"strings" "strings"
"text/tabwriter" "text/tabwriter"
"github.com/docker/docker/api/types"
Cli "github.com/docker/docker/cli" Cli "github.com/docker/docker/cli"
flag "github.com/docker/docker/pkg/mflag" flag "github.com/docker/docker/pkg/mflag"
) )
@ -21,23 +18,16 @@ func (cli *DockerCli) CmdTop(args ...string) error {
cmd.ParseFlags(args, true) cmd.ParseFlags(args, true)
val := url.Values{} var arguments []string
if cmd.NArg() > 1 { 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 { if err != nil {
return err 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) w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
fmt.Fprintln(w, strings.Join(procList.Titles, "\t")) fmt.Fprintln(w, strings.Join(procList.Titles, "\t"))