import.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package client
  2. import (
  3. "io"
  4. "os"
  5. "golang.org/x/net/context"
  6. Cli "github.com/docker/docker/cli"
  7. "github.com/docker/docker/opts"
  8. "github.com/docker/docker/pkg/jsonmessage"
  9. flag "github.com/docker/docker/pkg/mflag"
  10. "github.com/docker/docker/pkg/urlutil"
  11. "github.com/docker/engine-api/types"
  12. )
  13. // CmdImport creates an empty filesystem image, imports the contents of the tarball into the image, and optionally tags the image.
  14. //
  15. // 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.
  16. //
  17. // Usage: docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
  18. func (cli *DockerCli) CmdImport(args ...string) error {
  19. cmd := Cli.Subcmd("import", []string{"file|URL|- [REPOSITORY[:TAG]]"}, Cli.DockerCommands["import"].Description, true)
  20. flChanges := opts.NewListOpts(nil)
  21. cmd.Var(&flChanges, []string{"c", "-change"}, "Apply Dockerfile instruction to the created image")
  22. message := cmd.String([]string{"m", "-message"}, "", "Set commit message for imported image")
  23. cmd.Require(flag.Min, 1)
  24. cmd.ParseFlags(args, true)
  25. var (
  26. in io.Reader
  27. src = cmd.Arg(0)
  28. srcName = src
  29. ref = cmd.Arg(1)
  30. changes = flChanges.GetAll()
  31. )
  32. if src == "-" {
  33. in = cli.in
  34. } else if !urlutil.IsURL(src) {
  35. srcName = "-"
  36. file, err := os.Open(src)
  37. if err != nil {
  38. return err
  39. }
  40. defer file.Close()
  41. in = file
  42. }
  43. source := types.ImageImportSource{
  44. Source: in,
  45. SourceName: srcName,
  46. }
  47. options := types.ImageImportOptions{
  48. Message: *message,
  49. Changes: changes,
  50. }
  51. responseBody, err := cli.client.ImageImport(context.Background(), source, ref, options)
  52. if err != nil {
  53. return err
  54. }
  55. defer responseBody.Close()
  56. return jsonmessage.DisplayJSONMessagesStream(responseBody, cli.out, cli.outFd, cli.isTerminalOut, nil)
  57. }