Browse code

Implement docker load with standalone client lib.

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

David Calavera authored on 2015/12/04 08:49:55
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,17 @@
0
+package lib
1
+
2
+import (
3
+	"io"
4
+	"net/url"
5
+)
6
+
7
+// ImageLoad loads an image in the docker host from the client host.
8
+// It's up to the caller to close the io.ReadCloser returned by
9
+// this function.
10
+func (cli *Client) ImageLoad(input io.Reader) (io.ReadCloser, error) {
11
+	resp, err := cli.POSTRaw("/images/load", url.Values{}, input, nil)
12
+	if err != nil {
13
+		return nil, err
14
+	}
15
+	return resp.body, nil
16
+}
... ...
@@ -17,26 +17,24 @@ func (cli *DockerCli) CmdLoad(args ...string) error {
17 17
 	cmd := Cli.Subcmd("load", nil, Cli.DockerCommands["load"].Description, true)
18 18
 	infile := cmd.String([]string{"i", "-input"}, "", "Read from a tar archive file, instead of STDIN")
19 19
 	cmd.Require(flag.Exact, 0)
20
-
21 20
 	cmd.ParseFlags(args, true)
22 21
 
23
-	var (
24
-		input io.Reader = cli.in
25
-		err   error
26
-	)
22
+	var input io.Reader = cli.in
27 23
 	if *infile != "" {
28
-		input, err = os.Open(*infile)
24
+		file, err := os.Open(*infile)
29 25
 		if err != nil {
30 26
 			return err
31 27
 		}
28
+		defer file.Close()
29
+		input = file
32 30
 	}
33
-	sopts := &streamOpts{
34
-		rawTerminal: true,
35
-		in:          input,
36
-		out:         cli.out,
37
-	}
38
-	if _, err := cli.stream("POST", "/images/load", sopts); err != nil {
31
+
32
+	responseBody, err := cli.client.ImageLoad(input)
33
+	if err != nil {
39 34
 		return err
40 35
 	}
41
-	return nil
36
+	defer responseBody.Close()
37
+
38
+	_, err = io.Copy(cli.out, responseBody)
39
+	return err
42 40
 }