github.com/circular-dark/docker@v1.7.0/api/client/import.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"net/url"
     7  
     8  	"github.com/docker/docker/opts"
     9  	flag "github.com/docker/docker/pkg/mflag"
    10  	"github.com/docker/docker/pkg/parsers"
    11  	"github.com/docker/docker/registry"
    12  )
    13  
    14  // CmdImport creates an empty filesystem image, imports the contents of the tarball into the image, and optionally tags the image.
    15  //
    16  // The URL argument is the address of a tarball (.tar, .tar.gz, .tgz, .bzip, .tar.xz, .txz) file. If the URL is '-', then the tar file is read from STDIN.
    17  //
    18  // Usage: docker import [OPTIONS] URL [REPOSITORY[:TAG]]
    19  func (cli *DockerCli) CmdImport(args ...string) error {
    20  	cmd := cli.Subcmd("import", "URL|- [REPOSITORY[:TAG]]", "Create an empty filesystem image and import the contents of the\ntarball (.tar, .tar.gz, .tgz, .bzip, .tar.xz, .txz) into it, then\noptionally tag it.", true)
    21  	flChanges := opts.NewListOpts(nil)
    22  	cmd.Var(&flChanges, []string{"c", "-change"}, "Apply Dockerfile instruction to the created image")
    23  	cmd.Require(flag.Min, 1)
    24  
    25  	cmd.ParseFlags(args, true)
    26  
    27  	var (
    28  		v          = url.Values{}
    29  		src        = cmd.Arg(0)
    30  		repository = cmd.Arg(1)
    31  	)
    32  
    33  	v.Set("fromSrc", src)
    34  	v.Set("repo", repository)
    35  	for _, change := range flChanges.GetAll() {
    36  		v.Add("changes", change)
    37  	}
    38  	if cmd.NArg() == 3 {
    39  		fmt.Fprintf(cli.err, "[DEPRECATED] The format 'URL|- [REPOSITORY [TAG]]' has been deprecated. Please use URL|- [REPOSITORY[:TAG]]\n")
    40  		v.Set("tag", cmd.Arg(2))
    41  	}
    42  
    43  	if repository != "" {
    44  		//Check if the given image name can be resolved
    45  		repo, _ := parsers.ParseRepositoryTag(repository)
    46  		if err := registry.ValidateRepositoryName(repo); err != nil {
    47  			return err
    48  		}
    49  	}
    50  
    51  	var in io.Reader
    52  
    53  	if src == "-" {
    54  		in = cli.in
    55  	}
    56  
    57  	sopts := &streamOpts{
    58  		rawTerminal: true,
    59  		in:          in,
    60  		out:         cli.out,
    61  	}
    62  
    63  	return cli.stream("POST", "/images/create?"+v.Encode(), sopts)
    64  }