import.go 2.3 KB

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