save.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package client
  2. import (
  3. "errors"
  4. "io"
  5. Cli "github.com/docker/docker/cli"
  6. flag "github.com/docker/docker/pkg/mflag"
  7. )
  8. // CmdSave saves one or more images to a tar archive.
  9. //
  10. // The tar archive is written to STDOUT by default, or written to a file.
  11. //
  12. // Usage: docker save [OPTIONS] IMAGE [IMAGE...]
  13. func (cli *DockerCli) CmdSave(args ...string) error {
  14. cmd := Cli.Subcmd("save", []string{"IMAGE [IMAGE...]"}, Cli.DockerCommands["save"].Description+" (streamed to STDOUT by default)", true)
  15. outfile := cmd.String([]string{"o", "-output"}, "", "Write to a file, instead of STDOUT")
  16. cmd.Require(flag.Min, 1)
  17. cmd.ParseFlags(args, true)
  18. if *outfile == "" && cli.isTerminalOut {
  19. return errors.New("Cowardly refusing to save to a terminal. Use the -o flag or redirect.")
  20. }
  21. responseBody, err := cli.client.ImageSave(cmd.Args())
  22. if err != nil {
  23. return err
  24. }
  25. defer responseBody.Close()
  26. if *outfile == "" {
  27. _, err := io.Copy(cli.out, responseBody)
  28. return err
  29. }
  30. return copyToFile(*outfile, responseBody)
  31. }