export.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package client
  2. import (
  3. "errors"
  4. "io"
  5. "golang.org/x/net/context"
  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. if *outfile == "" && cli.isTerminalOut {
  20. return errors.New("Cowardly refusing to save to a terminal. Use the -o flag or redirect.")
  21. }
  22. responseBody, err := cli.client.ContainerExport(context.Background(), cmd.Arg(0))
  23. if err != nil {
  24. return err
  25. }
  26. defer responseBody.Close()
  27. if *outfile == "" {
  28. _, err := io.Copy(cli.out, responseBody)
  29. return err
  30. }
  31. return copyToFile(*outfile, responseBody)
  32. }