ソースを参照

Implement docker remove with standalone client lib.

Signed-off-by: David Calavera <david.calavera@gmail.com>
David Calavera 9 年 前
コミット
fb6533e6cf
2 ファイル変更39 行追加15 行削除
  1. 30 0
      api/client/lib/container_remove.go
  2. 9 15
      api/client/rm.go

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

+ 9 - 15
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 {