github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/api/client/export.go (about)

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