export.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package client
  2. import (
  3. "errors"
  4. "io"
  5. "net/url"
  6. "os"
  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", "CONTAINER", "Export a filesystem as a tar archive (streamed to STDOUT by default)", 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. if len(cmd.Args()) == 1 {
  32. image := cmd.Arg(0)
  33. if err := cli.stream("GET", "/containers/"+image+"/export", nil, output, nil); err != nil {
  34. return err
  35. }
  36. } else {
  37. v := url.Values{}
  38. for _, arg := range cmd.Args() {
  39. v.Add("names", arg)
  40. }
  41. if err := cli.stream("GET", "/containers/get?"+v.Encode(), nil, output, nil); err != nil {
  42. return err
  43. }
  44. }
  45. return nil
  46. }