github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/api/client/import.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  
     8  	Cli "github.com/docker/docker/cli"
     9  	"github.com/docker/docker/opts"
    10  	"github.com/docker/docker/pkg/jsonmessage"
    11  	flag "github.com/docker/docker/pkg/mflag"
    12  	"github.com/docker/docker/pkg/urlutil"
    13  	"github.com/docker/docker/reference"
    14  	"github.com/docker/engine-api/types"
    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  		in         io.Reader
    33  		tag        string
    34  		src        = cmd.Arg(0)
    35  		srcName    = src
    36  		repository = cmd.Arg(1)
    37  		changes    = flChanges.GetAll()
    38  	)
    39  
    40  	if cmd.NArg() == 3 {
    41  		fmt.Fprintf(cli.err, "[DEPRECATED] The format 'file|URL|- [REPOSITORY [TAG]]' has been deprecated. Please use file|URL|- [REPOSITORY[:TAG]]\n")
    42  		tag = cmd.Arg(2)
    43  	}
    44  
    45  	if repository != "" {
    46  		//Check if the given image name can be resolved
    47  		if _, err := reference.ParseNamed(repository); err != nil {
    48  			return err
    49  		}
    50  	}
    51  
    52  	if src == "-" {
    53  		in = cli.in
    54  	} else if !urlutil.IsURL(src) {
    55  		srcName = "-"
    56  		file, err := os.Open(src)
    57  		if err != nil {
    58  			return err
    59  		}
    60  		defer file.Close()
    61  		in = file
    62  	}
    63  
    64  	options := types.ImageImportOptions{
    65  		Source:         in,
    66  		SourceName:     srcName,
    67  		RepositoryName: repository,
    68  		Message:        *message,
    69  		Tag:            tag,
    70  		Changes:        changes,
    71  	}
    72  
    73  	responseBody, err := cli.client.ImageImport(options)
    74  	if err != nil {
    75  		return err
    76  	}
    77  	defer responseBody.Close()
    78  
    79  	return jsonmessage.DisplayJSONMessagesStream(responseBody, cli.out, cli.outFd, cli.isTerminalOut, nil)
    80  }