github.com/tompao/docker@v1.9.1/api/client/export.go (about)

     1  package client
     2  
     3  import (
     4  	"errors"
     5  	"os"
     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  	var (
    24  		output = 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  }