github.com/a4a881d4/docker@v1.9.0-rc2/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]]"}, Cli.DockerCommands["import"].Description, true)
    24  	flChanges := opts.NewListOpts(nil)
    25  	cmd.Var(&flChanges, []string{"c", "-change"}, "Apply Dockerfile instruction to the created image")
    26  	message := cmd.String([]string{"m", "-message"}, "", "Set commit message for imported image")
    27  	cmd.Require(flag.Min, 1)
    28  
    29  	cmd.ParseFlags(args, true)
    30  
    31  	var (
    32  		v          = url.Values{}
    33  		src        = cmd.Arg(0)
    34  		repository = cmd.Arg(1)
    35  	)
    36  
    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  	if cmd.NArg() == 3 {
    44  		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))
    46  	}
    47  
    48  	if repository != "" {
    49  		//Check if the given image name can be resolved
    50  		repo, _ := parsers.ParseRepositoryTag(repository)
    51  		if err := registry.ValidateRepositoryName(repo); err != nil {
    52  			return err
    53  		}
    54  	}
    55  
    56  	var in io.Reader
    57  
    58  	if src == "-" {
    59  		in = cli.in
    60  	} else if !urlutil.IsURL(src) {
    61  		v.Set("fromSrc", "-")
    62  		file, err := os.Open(src)
    63  		if err != nil {
    64  			return err
    65  		}
    66  		defer file.Close()
    67  		in = file
    68  
    69  	}
    70  
    71  	sopts := &streamOpts{
    72  		rawTerminal: true,
    73  		in:          in,
    74  		out:         cli.out,
    75  	}
    76  
    77  	_, err := cli.stream("POST", "/images/create?"+v.Encode(), sopts)
    78  	return err
    79  }