github.com/circular-dark/docker@v1.7.0/api/client/export.go (about)

     1  package client
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  	"os"
     7  
     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", "CONTAINER", "Export a filesystem as a tar archive (streamed to STDOUT by default)", 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  	var (
    24  		output io.Writer = cli.out
    25  		err    error
    26  	)
    27  	if *outfile != "" {
    28  		output, err = os.Create(*outfile)
    29  		if err != nil {
    30  			return err
    31  		}
    32  	} else if cli.isTerminalOut {
    33  		return errors.New("Cowardly refusing to save to a terminal. Use the -o flag or redirect.")
    34  	}
    35  
    36  	image := cmd.Arg(0)
    37  	sopts := &streamOpts{
    38  		rawTerminal: true,
    39  		out:         output,
    40  	}
    41  	if err := cli.stream("GET", "/containers/"+image+"/export", sopts); err != nil {
    42  		return err
    43  	}
    44  
    45  	return nil
    46  }