Browse code

Implement docker export with standalone client lib.

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

David Calavera authored on 2015/12/04 04:49:41
Showing 2 changed files
... ...
@@ -2,6 +2,7 @@ package client
2 2
 
3 3
 import (
4 4
 	"errors"
5
+	"io"
5 6
 	"os"
6 7
 
7 8
 	Cli "github.com/docker/docker/cli"
... ...
@@ -33,14 +34,12 @@ func (cli *DockerCli) CmdExport(args ...string) error {
33 33
 		return errors.New("Cowardly refusing to save to a terminal. Use the -o flag or redirect.")
34 34
 	}
35 35
 
36
-	image := cmd.Arg(0)
37
-	sopts := &streamOpts{
38
-		rawTerminal: true,
39
-		out:         output,
40
-	}
41
-	if _, err := cli.stream("GET", "/containers/"+image+"/export", sopts); err != nil {
36
+	responseBody, err := cli.client.ContainerExport(cmd.Arg(0))
37
+	if err != nil {
42 38
 		return err
43 39
 	}
40
+	defer responseBody.Close()
44 41
 
45
-	return nil
42
+	_, err = io.Copy(output, responseBody)
43
+	return err
46 44
 }
47 45
new file mode 100644
... ...
@@ -0,0 +1,18 @@
0
+package lib
1
+
2
+import (
3
+	"io"
4
+	"net/url"
5
+)
6
+
7
+// ContainerExport retrieves the raw contents of a container
8
+// and returns them as a io.ReadCloser. It's up to the caller
9
+// to close the stream.
10
+func (cli *Client) ContainerExport(containerID string) (io.ReadCloser, error) {
11
+	serverResp, err := cli.GET("/containers/"+containerID+"/export", url.Values{}, nil)
12
+	if err != nil {
13
+		return nil, err
14
+	}
15
+
16
+	return serverResp.body, nil
17
+}