import.go 1.8 KB

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