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