import.go 2.2 KB

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