export.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package client
  2. import (
  3. "errors"
  4. "io"
  5. "os"
  6. Cli "github.com/docker/docker/cli"
  7. flag "github.com/docker/docker/pkg/mflag"
  8. )
  9. // CmdExport exports a filesystem as a tar archive.
  10. //
  11. // The tar archive is streamed to STDOUT by default or written to a file.
  12. //
  13. // Usage: docker export [OPTIONS] CONTAINER
  14. func (cli *DockerCli) CmdExport(args ...string) error {
  15. cmd := Cli.Subcmd("export", []string{"CONTAINER"}, Cli.DockerCommands["export"].Description, true)
  16. outfile := cmd.String([]string{"o", "-output"}, "", "Write to a file, instead of STDOUT")
  17. cmd.Require(flag.Exact, 1)
  18. cmd.ParseFlags(args, true)
  19. var (
  20. output = cli.out
  21. err error
  22. )
  23. if *outfile != "" {
  24. output, err = os.Create(*outfile)
  25. if err != nil {
  26. return err
  27. }
  28. } else if cli.isTerminalOut {
  29. return errors.New("Cowardly refusing to save to a terminal. Use the -o flag or redirect.")
  30. }
  31. responseBody, err := cli.client.ContainerExport(cmd.Arg(0))
  32. if err != nil {
  33. return err
  34. }
  35. defer responseBody.Close()
  36. _, err = io.Copy(output, responseBody)
  37. return err
  38. }