export.go 1.3 KB

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