github.com/kunnos/engine@v1.13.1/cli/command/container/export.go (about) 1 package container 2 3 import ( 4 "errors" 5 "io" 6 7 "golang.org/x/net/context" 8 9 "github.com/docker/docker/cli" 10 "github.com/docker/docker/cli/command" 11 "github.com/spf13/cobra" 12 ) 13 14 type exportOptions struct { 15 container string 16 output string 17 } 18 19 // NewExportCommand creates a new `docker export` command 20 func NewExportCommand(dockerCli *command.DockerCli) *cobra.Command { 21 var opts exportOptions 22 23 cmd := &cobra.Command{ 24 Use: "export [OPTIONS] CONTAINER", 25 Short: "Export a container's filesystem as a tar archive", 26 Args: cli.ExactArgs(1), 27 RunE: func(cmd *cobra.Command, args []string) error { 28 opts.container = args[0] 29 return runExport(dockerCli, opts) 30 }, 31 } 32 33 flags := cmd.Flags() 34 35 flags.StringVarP(&opts.output, "output", "o", "", "Write to a file, instead of STDOUT") 36 37 return cmd 38 } 39 40 func runExport(dockerCli *command.DockerCli, opts exportOptions) error { 41 if opts.output == "" && dockerCli.Out().IsTerminal() { 42 return errors.New("Cowardly refusing to save to a terminal. Use the -o flag or redirect.") 43 } 44 45 clnt := dockerCli.Client() 46 47 responseBody, err := clnt.ContainerExport(context.Background(), opts.container) 48 if err != nil { 49 return err 50 } 51 defer responseBody.Close() 52 53 if opts.output == "" { 54 _, err := io.Copy(dockerCli.Out(), responseBody) 55 return err 56 } 57 58 return command.CopyToFile(opts.output, responseBody) 59 }