Bläddra i källkod

Implement docker import with the standalone client lib.

Signed-off-by: David Calavera <david.calavera@gmail.com>
David Calavera 9 år sedan
förälder
incheckning
6bf757500b
3 ändrade filer med 64 tillägg och 19 borttagningar
  1. 22 18
      api/client/import.go
  2. 1 1
      api/client/lib/image_create.go
  3. 41 0
      api/client/lib/image_import.go

+ 22 - 18
api/client/import.go

@@ -3,12 +3,13 @@ package client
 import (
 	"fmt"
 	"io"
-	"net/url"
 	"os"
 
 	"github.com/docker/distribution/reference"
+	"github.com/docker/docker/api/client/lib"
 	Cli "github.com/docker/docker/cli"
 	"github.com/docker/docker/opts"
+	"github.com/docker/docker/pkg/jsonmessage"
 	flag "github.com/docker/docker/pkg/mflag"
 	"github.com/docker/docker/pkg/urlutil"
 	"github.com/docker/docker/registry"
@@ -29,20 +30,17 @@ func (cli *DockerCli) CmdImport(args ...string) error {
 	cmd.ParseFlags(args, true)
 
 	var (
-		v          = url.Values{}
+		in         io.Reader
+		tag        string
 		src        = cmd.Arg(0)
+		srcName    = src
 		repository = cmd.Arg(1)
+		changes    = flChanges.GetAll()
 	)
 
-	v.Set("fromSrc", src)
-	v.Set("repo", repository)
-	v.Set("message", *message)
-	for _, change := range flChanges.GetAll() {
-		v.Add("changes", change)
-	}
 	if cmd.NArg() == 3 {
 		fmt.Fprintf(cli.err, "[DEPRECATED] The format 'file|URL|- [REPOSITORY [TAG]]' has been deprecated. Please use file|URL|- [REPOSITORY[:TAG]]\n")
-		v.Set("tag", cmd.Arg(2))
+		tag = cmd.Arg(2)
 	}
 
 	if repository != "" {
@@ -56,12 +54,10 @@ func (cli *DockerCli) CmdImport(args ...string) error {
 		}
 	}
 
-	var in io.Reader
-
 	if src == "-" {
 		in = cli.in
 	} else if !urlutil.IsURL(src) {
-		v.Set("fromSrc", "-")
+		srcName = "-"
 		file, err := os.Open(src)
 		if err != nil {
 			return err
@@ -71,12 +67,20 @@ func (cli *DockerCli) CmdImport(args ...string) error {
 
 	}
 
-	sopts := &streamOpts{
-		rawTerminal: true,
-		in:          in,
-		out:         cli.out,
+	options := lib.ImportImageOptions{
+		Source:         in,
+		SourceName:     srcName,
+		RepositoryName: repository,
+		Message:        *message,
+		Tag:            tag,
+		Changes:        changes,
+	}
+
+	responseBody, err := cli.client.ImportImage(options)
+	if err != nil {
+		return err
 	}
+	defer responseBody.Close()
 
-	_, err := cli.stream("POST", "/images/create?"+v.Encode(), sopts)
-	return err
+	return jsonmessage.DisplayJSONMessagesStream(responseBody, cli.out, cli.outFd, cli.isTerminalOut)
 }

+ 1 - 1
api/client/lib/image_create.go

@@ -5,7 +5,7 @@ import (
 	"net/url"
 )
 
-// CreateImageOptions holds information to create images
+// CreateImageOptions holds information to create images.
 type CreateImageOptions struct {
 	// Parent is the image to create this image from
 	Parent string

+ 41 - 0
api/client/lib/image_import.go

@@ -0,0 +1,41 @@
+package lib
+
+import (
+	"io"
+	"net/url"
+)
+
+// ImportImageOptions holds information to import images from the client host.
+type ImportImageOptions struct {
+	// Source is the data to send to the server to create this image from
+	Source io.Reader
+	// Source is the name of the source to import this image from
+	SourceName string
+	// RepositoryName is the name of the repository to import this image
+	RepositoryName string
+	// Message is the message to tag the image with
+	Message string
+	// Tag is the name to tag this image
+	Tag string
+	// Changes are the raw changes to apply to the image
+	Changes []string
+}
+
+// ImportImage creates a new image based in the source options.
+// It returns the JSON content in the response body.
+func (cli *Client) ImportImage(options ImportImageOptions) (io.ReadCloser, error) {
+	var query url.Values
+	query.Set("fromSrc", options.SourceName)
+	query.Set("repo", options.RepositoryName)
+	query.Set("tag", options.Tag)
+	query.Set("message", options.Message)
+	for _, change := range options.Changes {
+		query.Add("changes", change)
+	}
+
+	resp, err := cli.POSTRaw("/images/create", query, options.Source, nil)
+	if err != nil {
+		return nil, err
+	}
+	return resp.body, nil
+}