Browse code

Implement docker wait with standalone client lib.

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

David Calavera authored on 2015/12/05 04:38:15
Showing 4 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,24 @@
0
+package lib
1
+
2
+import (
3
+	"encoding/json"
4
+
5
+	"github.com/docker/docker/api/types"
6
+)
7
+
8
+// ContainerWait pauses execution util a container is exits.
9
+// It returns the API status code as response of its readiness.
10
+func (cli *Client) ContainerWait(containerID string) (int, error) {
11
+	resp, err := cli.post("/containers/"+containerID+"/wait", nil, nil, nil)
12
+	if err != nil {
13
+		return -1, err
14
+	}
15
+	defer ensureReaderClosed(resp)
16
+
17
+	var res types.ContainerWaitResponse
18
+	if err := json.NewDecoder(resp.body).Decode(&res); err != nil {
19
+		return -1, err
20
+	}
21
+
22
+	return res.StatusCode, nil
23
+}
... ...
@@ -272,7 +272,7 @@ func (cli *DockerCli) CmdRun(args ...string) error {
272 272
 		// No Autoremove: Simply retrieve the exit code
273 273
 		if !config.Tty {
274 274
 			// In non-TTY mode, we can't detach, so we must wait for container exit
275
-			if status, err = waitForExit(cli, createResponse.ID); err != nil {
275
+			if status, err = cli.client.ContainerWait(createResponse.ID); err != nil {
276 276
 				return err
277 277
 			}
278 278
 		} else {
... ...
@@ -260,22 +260,6 @@ func (cli *DockerCli) resizeTty(id string, isExec bool) {
260 260
 	}
261 261
 }
262 262
 
263
-func waitForExit(cli *DockerCli, containerID string) (int, error) {
264
-	serverResp, err := cli.call("POST", "/containers/"+containerID+"/wait", nil, nil)
265
-	if err != nil {
266
-		return -1, err
267
-	}
268
-
269
-	defer serverResp.body.Close()
270
-
271
-	var res types.ContainerWaitResponse
272
-	if err := json.NewDecoder(serverResp.body).Decode(&res); err != nil {
273
-		return -1, err
274
-	}
275
-
276
-	return res.StatusCode, nil
277
-}
278
-
279 263
 // getExitCode perform an inspect on the container. It returns
280 264
 // the running state and the exit code.
281 265
 func getExitCode(cli *DockerCli, containerID string) (bool, int, error) {
... ...
@@ -20,7 +20,7 @@ func (cli *DockerCli) CmdWait(args ...string) error {
20 20
 
21 21
 	var errNames []string
22 22
 	for _, name := range cmd.Args() {
23
-		status, err := waitForExit(cli, name)
23
+		status, err := cli.client.ContainerWait(name)
24 24
 		if err != nil {
25 25
 			fmt.Fprintf(cli.err, "%s\n", err)
26 26
 			errNames = append(errNames, name)