import.go 2.2 KB

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