github.com/hamo/docker@v1.11.1/api/client/import.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  
     8  	"golang.org/x/net/context"
     9  
    10  	Cli "github.com/docker/docker/cli"
    11  	"github.com/docker/docker/opts"
    12  	"github.com/docker/docker/pkg/jsonmessage"
    13  	flag "github.com/docker/docker/pkg/mflag"
    14  	"github.com/docker/docker/pkg/urlutil"
    15  	"github.com/docker/docker/reference"
    16  	"github.com/docker/engine-api/types"
    17  )
    18  
    19  // CmdImport creates an empty filesystem image, imports the contents of the tarball into the image, and optionally tags the image.
    20  //
    21  // The URL argument is the address of a tarball (.tar, .tar.gz, .tgz, .bzip, .tar.xz, .txz) file or a path to local file relative to docker client. If the URL is '-', then the tar file is read from STDIN.
    22  //
    23  // Usage: docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
    24  func (cli *DockerCli) CmdImport(args ...string) error {
    25  	cmd := Cli.Subcmd("import", []string{"file|URL|- [REPOSITORY[:TAG]]"}, Cli.DockerCommands["import"].Description, true)
    26  	flChanges := opts.NewListOpts(nil)
    27  	cmd.Var(&flChanges, []string{"c", "-change"}, "Apply Dockerfile instruction to the created image")
    28  	message := cmd.String([]string{"m", "-message"}, "", "Set commit message for imported image")
    29  	cmd.Require(flag.Min, 1)
    30  
    31  	cmd.ParseFlags(args, true)
    32  
    33  	var (
    34  		in         io.Reader
    35  		tag        string
    36  		src        = cmd.Arg(0)
    37  		srcName    = src
    38  		repository = cmd.Arg(1)
    39  		changes    = flChanges.GetAll()
    40  	)
    41  
    42  	if cmd.NArg() == 3 {
    43  		fmt.Fprintf(cli.err, "[DEPRECATED] The format 'file|URL|- [REPOSITORY [TAG]]' has been deprecated. Please use file|URL|- [REPOSITORY[:TAG]]\n")
    44  		tag = cmd.Arg(2)
    45  	}
    46  
    47  	if repository != "" {
    48  		//Check if the given image name can be resolved
    49  		if _, err := reference.ParseNamed(repository); err != nil {
    50  			return err
    51  		}
    52  	}
    53  
    54  	if src == "-" {
    55  		in = cli.in
    56  	} else if !urlutil.IsURL(src) {
    57  		srcName = "-"
    58  		file, err := os.Open(src)
    59  		if err != nil {
    60  			return err
    61  		}
    62  		defer file.Close()
    63  		in = file
    64  	}
    65  
    66  	options := types.ImageImportOptions{
    67  		Source:         in,
    68  		SourceName:     srcName,
    69  		RepositoryName: repository,
    70  		Message:        *message,
    71  		Tag:            tag,
    72  		Changes:        changes,
    73  	}
    74  
    75  	responseBody, err := cli.client.ImageImport(context.Background(), options)
    76  	if err != nil {
    77  		return err
    78  	}
    79  	defer responseBody.Close()
    80  
    81  	return jsonmessage.DisplayJSONMessagesStream(responseBody, cli.out, cli.outFd, cli.isTerminalOut, nil)
    82  }