From fb6533e6cf1793b653ab0ea9f12b6b6876076d97 Mon Sep 17 00:00:00 2001 From: David Calavera Date: Fri, 4 Dec 2015 12:53:57 -0500 Subject: [PATCH] Implement docker remove with standalone client lib. Signed-off-by: David Calavera --- api/client/lib/container_remove.go | 30 ++++++++++++++++++++++++++++++ api/client/rm.go | 24 +++++++++--------------- 2 files changed, 39 insertions(+), 15 deletions(-) create mode 100644 api/client/lib/container_remove.go diff --git a/api/client/lib/container_remove.go b/api/client/lib/container_remove.go new file mode 100644 index 0000000000..3c2b0a24b4 --- /dev/null +++ b/api/client/lib/container_remove.go @@ -0,0 +1,30 @@ +package lib + +import "net/url" + +// ContainerRemoveOptions holds parameters to remove containers. +type ContainerRemoveOptions struct { + ContainerID string + RemoveVolumes bool + RemoveLinks bool + Force bool +} + +// ContainerRemove kills and removes a container from the docker host. +func (cli *Client) ContainerRemove(options ContainerRemoveOptions) error { + var query url.Values + if options.RemoveVolumes { + query.Set("v", "1") + } + if options.RemoveLinks { + query.Set("link", "1") + } + + if options.Force { + query.Set("force", "1") + } + + resp, err := cli.DELETE("/containers/"+options.ContainerID, query, nil) + ensureReaderClosed(resp) + return err +} diff --git a/api/client/rm.go b/api/client/rm.go index cf88c765d3..cf92e889b7 100644 --- a/api/client/rm.go +++ b/api/client/rm.go @@ -2,9 +2,9 @@ package client import ( "fmt" - "net/url" "strings" + "github.com/docker/docker/api/client/lib" Cli "github.com/docker/docker/cli" flag "github.com/docker/docker/pkg/mflag" ) @@ -21,18 +21,6 @@ func (cli *DockerCli) CmdRm(args ...string) error { cmd.ParseFlags(args, true) - val := url.Values{} - if *v { - val.Set("v", "1") - } - if *link { - val.Set("link", "1") - } - - if *force { - val.Set("force", "1") - } - var errNames []string for _, name := range cmd.Args() { if name == "" { @@ -40,8 +28,14 @@ func (cli *DockerCli) CmdRm(args ...string) error { } name = strings.Trim(name, "/") - _, _, err := readBody(cli.call("DELETE", "/containers/"+name+"?"+val.Encode(), nil, nil)) - if err != nil { + options := lib.ContainerRemoveOptions{ + ContainerID: name, + RemoveVolumes: *v, + RemoveLinks: *link, + Force: *force, + } + + if err := cli.client.ContainerRemove(options); err != nil { fmt.Fprintf(cli.err, "%s\n", err) errNames = append(errNames, name) } else {