github.com/hustcat/docker@v1.3.3-0.20160314103604-901c67a8eeab/api/client/export.go (about) 1 package client 2 3 import ( 4 "errors" 5 "io" 6 7 "golang.org/x/net/context" 8 9 Cli "github.com/docker/docker/cli" 10 flag "github.com/docker/docker/pkg/mflag" 11 ) 12 13 // CmdExport exports a filesystem as a tar archive. 14 // 15 // The tar archive is streamed to STDOUT by default or written to a file. 16 // 17 // Usage: docker export [OPTIONS] CONTAINER 18 func (cli *DockerCli) CmdExport(args ...string) error { 19 cmd := Cli.Subcmd("export", []string{"CONTAINER"}, Cli.DockerCommands["export"].Description, true) 20 outfile := cmd.String([]string{"o", "-output"}, "", "Write to a file, instead of STDOUT") 21 cmd.Require(flag.Exact, 1) 22 23 cmd.ParseFlags(args, true) 24 25 if *outfile == "" && cli.isTerminalOut { 26 return errors.New("Cowardly refusing to save to a terminal. Use the -o flag or redirect.") 27 } 28 29 responseBody, err := cli.client.ContainerExport(context.Background(), cmd.Arg(0)) 30 if err != nil { 31 return err 32 } 33 defer responseBody.Close() 34 35 if *outfile == "" { 36 _, err := io.Copy(cli.out, responseBody) 37 return err 38 } 39 40 return copyToFile(*outfile, responseBody) 41 42 }