save.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package client
  2. import (
  3. "errors"
  4. "net/url"
  5. "os"
  6. Cli "github.com/docker/docker/cli"
  7. flag "github.com/docker/docker/pkg/mflag"
  8. )
  9. // CmdSave saves one or more images to a tar archive.
  10. //
  11. // The tar archive is written to STDOUT by default, or written to a file.
  12. //
  13. // Usage: docker save [OPTIONS] IMAGE [IMAGE...]
  14. func (cli *DockerCli) CmdSave(args ...string) error {
  15. cmd := Cli.Subcmd("save", []string{"IMAGE [IMAGE...]"}, "Save an image(s) to a tar archive (streamed to STDOUT by default)", true)
  16. outfile := cmd.String([]string{"o", "-output"}, "", "Write to a file, instead of STDOUT")
  17. cmd.Require(flag.Min, 1)
  18. cmd.ParseFlags(args, true)
  19. var (
  20. output = cli.out
  21. err error
  22. )
  23. if *outfile == "" && cli.isTerminalOut {
  24. return errors.New("Cowardly refusing to save to a terminal. Use the -o flag or redirect.")
  25. }
  26. if *outfile != "" {
  27. if output, err = os.Create(*outfile); err != nil {
  28. return err
  29. }
  30. }
  31. sopts := &streamOpts{
  32. rawTerminal: true,
  33. out: output,
  34. }
  35. v := url.Values{}
  36. for _, arg := range cmd.Args() {
  37. v.Add("names", arg)
  38. }
  39. if _, err := cli.stream("GET", "/images/get?"+v.Encode(), sopts); err != nil {
  40. return err
  41. }
  42. return nil
  43. }