export.go 1015 B

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