github.com/rentongzhang/docker@v1.8.2-rc1/api/client/import.go (about)

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