Browse code

Implement docker remove with standalone client lib.

Signed-off-by: David Calavera <david.calavera@gmail.com>

David Calavera authored on 2015/12/05 02:53:57
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,30 @@
0
+package lib
1
+
2
+import "net/url"
3
+
4
+// ContainerRemoveOptions holds parameters to remove containers.
5
+type ContainerRemoveOptions struct {
6
+	ContainerID   string
7
+	RemoveVolumes bool
8
+	RemoveLinks   bool
9
+	Force         bool
10
+}
11
+
12
+// ContainerRemove kills and removes a container from the docker host.
13
+func (cli *Client) ContainerRemove(options ContainerRemoveOptions) error {
14
+	var query url.Values
15
+	if options.RemoveVolumes {
16
+		query.Set("v", "1")
17
+	}
18
+	if options.RemoveLinks {
19
+		query.Set("link", "1")
20
+	}
21
+
22
+	if options.Force {
23
+		query.Set("force", "1")
24
+	}
25
+
26
+	resp, err := cli.DELETE("/containers/"+options.ContainerID, query, nil)
27
+	ensureReaderClosed(resp)
28
+	return err
29
+}
... ...
@@ -2,9 +2,9 @@ package client
2 2
 
3 3
 import (
4 4
 	"fmt"
5
-	"net/url"
6 5
 	"strings"
7 6
 
7
+	"github.com/docker/docker/api/client/lib"
8 8
 	Cli "github.com/docker/docker/cli"
9 9
 	flag "github.com/docker/docker/pkg/mflag"
10 10
 )
... ...
@@ -21,18 +21,6 @@ func (cli *DockerCli) CmdRm(args ...string) error {
21 21
 
22 22
 	cmd.ParseFlags(args, true)
23 23
 
24
-	val := url.Values{}
25
-	if *v {
26
-		val.Set("v", "1")
27
-	}
28
-	if *link {
29
-		val.Set("link", "1")
30
-	}
31
-
32
-	if *force {
33
-		val.Set("force", "1")
34
-	}
35
-
36 24
 	var errNames []string
37 25
 	for _, name := range cmd.Args() {
38 26
 		if name == "" {
... ...
@@ -40,8 +28,14 @@ func (cli *DockerCli) CmdRm(args ...string) error {
40 40
 		}
41 41
 		name = strings.Trim(name, "/")
42 42
 
43
-		_, _, err := readBody(cli.call("DELETE", "/containers/"+name+"?"+val.Encode(), nil, nil))
44
-		if err != nil {
43
+		options := lib.ContainerRemoveOptions{
44
+			ContainerID:   name,
45
+			RemoveVolumes: *v,
46
+			RemoveLinks:   *link,
47
+			Force:         *force,
48
+		}
49
+
50
+		if err := cli.client.ContainerRemove(options); err != nil {
45 51
 			fmt.Fprintf(cli.err, "%s\n", err)
46 52
 			errNames = append(errNames, name)
47 53
 		} else {