github.com/endocode/docker@v1.4.2-0.20160113120958-46eb4700391e/api/client/export.go (about)

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