import.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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/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. ref = cmd.Arg(1)
  32. changes = flChanges.GetAll()
  33. )
  34. if cmd.NArg() == 3 {
  35. // FIXME(vdemeester) Which version has this been deprecated ? should we remove it ?
  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 src == "-" {
  40. in = cli.in
  41. } else if !urlutil.IsURL(src) {
  42. srcName = "-"
  43. file, err := os.Open(src)
  44. if err != nil {
  45. return err
  46. }
  47. defer file.Close()
  48. in = file
  49. }
  50. source := types.ImageImportSource{
  51. Source: in,
  52. SourceName: srcName,
  53. }
  54. options := types.ImageImportOptions{
  55. Message: *message,
  56. Tag: tag,
  57. Changes: changes,
  58. }
  59. responseBody, err := cli.client.ImageImport(context.Background(), source, ref, options)
  60. if err != nil {
  61. return err
  62. }
  63. defer responseBody.Close()
  64. return jsonmessage.DisplayJSONMessagesStream(responseBody, cli.out, cli.outFd, cli.isTerminalOut, nil)
  65. }