Browse code

Implement docker import with the standalone client lib.

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

David Calavera authored on 2015/12/04 07:11:37
Showing 3 changed files
... ...
@@ -3,12 +3,13 @@ package client
3 3
 import (
4 4
 	"fmt"
5 5
 	"io"
6
-	"net/url"
7 6
 	"os"
8 7
 
9 8
 	"github.com/docker/distribution/reference"
9
+	"github.com/docker/docker/api/client/lib"
10 10
 	Cli "github.com/docker/docker/cli"
11 11
 	"github.com/docker/docker/opts"
12
+	"github.com/docker/docker/pkg/jsonmessage"
12 13
 	flag "github.com/docker/docker/pkg/mflag"
13 14
 	"github.com/docker/docker/pkg/urlutil"
14 15
 	"github.com/docker/docker/registry"
... ...
@@ -29,20 +30,17 @@ func (cli *DockerCli) CmdImport(args ...string) error {
29 29
 	cmd.ParseFlags(args, true)
30 30
 
31 31
 	var (
32
-		v          = url.Values{}
32
+		in         io.Reader
33
+		tag        string
33 34
 		src        = cmd.Arg(0)
35
+		srcName    = src
34 36
 		repository = cmd.Arg(1)
37
+		changes    = flChanges.GetAll()
35 38
 	)
36 39
 
37
-	v.Set("fromSrc", src)
38
-	v.Set("repo", repository)
39
-	v.Set("message", *message)
40
-	for _, change := range flChanges.GetAll() {
41
-		v.Add("changes", change)
42
-	}
43 40
 	if cmd.NArg() == 3 {
44 41
 		fmt.Fprintf(cli.err, "[DEPRECATED] The format 'file|URL|- [REPOSITORY [TAG]]' has been deprecated. Please use file|URL|- [REPOSITORY[:TAG]]\n")
45
-		v.Set("tag", cmd.Arg(2))
42
+		tag = cmd.Arg(2)
46 43
 	}
47 44
 
48 45
 	if repository != "" {
... ...
@@ -56,12 +54,10 @@ func (cli *DockerCli) CmdImport(args ...string) error {
56 56
 		}
57 57
 	}
58 58
 
59
-	var in io.Reader
60
-
61 59
 	if src == "-" {
62 60
 		in = cli.in
63 61
 	} else if !urlutil.IsURL(src) {
64
-		v.Set("fromSrc", "-")
62
+		srcName = "-"
65 63
 		file, err := os.Open(src)
66 64
 		if err != nil {
67 65
 			return err
... ...
@@ -71,12 +67,20 @@ func (cli *DockerCli) CmdImport(args ...string) error {
71 71
 
72 72
 	}
73 73
 
74
-	sopts := &streamOpts{
75
-		rawTerminal: true,
76
-		in:          in,
77
-		out:         cli.out,
74
+	options := lib.ImportImageOptions{
75
+		Source:         in,
76
+		SourceName:     srcName,
77
+		RepositoryName: repository,
78
+		Message:        *message,
79
+		Tag:            tag,
80
+		Changes:        changes,
81
+	}
82
+
83
+	responseBody, err := cli.client.ImportImage(options)
84
+	if err != nil {
85
+		return err
78 86
 	}
87
+	defer responseBody.Close()
79 88
 
80
-	_, err := cli.stream("POST", "/images/create?"+v.Encode(), sopts)
81
-	return err
89
+	return jsonmessage.DisplayJSONMessagesStream(responseBody, cli.out, cli.outFd, cli.isTerminalOut)
82 90
 }
... ...
@@ -5,7 +5,7 @@ import (
5 5
 	"net/url"
6 6
 )
7 7
 
8
-// CreateImageOptions holds information to create images
8
+// CreateImageOptions holds information to create images.
9 9
 type CreateImageOptions struct {
10 10
 	// Parent is the image to create this image from
11 11
 	Parent string
12 12
new file mode 100644
... ...
@@ -0,0 +1,41 @@
0
+package lib
1
+
2
+import (
3
+	"io"
4
+	"net/url"
5
+)
6
+
7
+// ImportImageOptions holds information to import images from the client host.
8
+type ImportImageOptions struct {
9
+	// Source is the data to send to the server to create this image from
10
+	Source io.Reader
11
+	// Source is the name of the source to import this image from
12
+	SourceName string
13
+	// RepositoryName is the name of the repository to import this image
14
+	RepositoryName string
15
+	// Message is the message to tag the image with
16
+	Message string
17
+	// Tag is the name to tag this image
18
+	Tag string
19
+	// Changes are the raw changes to apply to the image
20
+	Changes []string
21
+}
22
+
23
+// ImportImage creates a new image based in the source options.
24
+// It returns the JSON content in the response body.
25
+func (cli *Client) ImportImage(options ImportImageOptions) (io.ReadCloser, error) {
26
+	var query url.Values
27
+	query.Set("fromSrc", options.SourceName)
28
+	query.Set("repo", options.RepositoryName)
29
+	query.Set("tag", options.Tag)
30
+	query.Set("message", options.Message)
31
+	for _, change := range options.Changes {
32
+		query.Add("changes", change)
33
+	}
34
+
35
+	resp, err := cli.POSTRaw("/images/create", query, options.Source, nil)
36
+	if err != nil {
37
+		return nil, err
38
+	}
39
+	return resp.body, nil
40
+}