export.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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"}, "Export the contents of a container's filesystem as a tar archive", 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 io.Writer = 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. image := cmd.Arg(0)
  32. sopts := &streamOpts{
  33. rawTerminal: true,
  34. out: output,
  35. }
  36. if _, err := cli.stream("GET", "/containers/"+image+"/export", sopts); err != nil {
  37. return err
  38. }
  39. return nil
  40. }