github.com/endocode/docker@v1.4.2-0.20160113120958-46eb4700391e/api/client/save.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  // CmdSave saves one or more images to a tar archive.
    13  //
    14  // The tar archive is written to STDOUT by default, or written to a file.
    15  //
    16  // Usage: docker save [OPTIONS] IMAGE [IMAGE...]
    17  func (cli *DockerCli) CmdSave(args ...string) error {
    18  	cmd := Cli.Subcmd("save", []string{"IMAGE [IMAGE...]"}, Cli.DockerCommands["save"].Description+" (streamed to STDOUT by default)", true)
    19  	outfile := cmd.String([]string{"o", "-output"}, "", "Write to a file, instead of STDOUT")
    20  	cmd.Require(flag.Min, 1)
    21  
    22  	cmd.ParseFlags(args, true)
    23  
    24  	var (
    25  		output = cli.out
    26  		err    error
    27  	)
    28  
    29  	if *outfile == "" && cli.isTerminalOut {
    30  		return errors.New("Cowardly refusing to save to a terminal. Use the -o flag or redirect.")
    31  	}
    32  	if *outfile != "" {
    33  		if output, err = os.Create(*outfile); err != nil {
    34  			return err
    35  		}
    36  	}
    37  
    38  	responseBody, err := cli.client.ImageSave(cmd.Args())
    39  	if err != nil {
    40  		return err
    41  	}
    42  	defer responseBody.Close()
    43  
    44  	_, err = io.Copy(output, responseBody)
    45  	return err
    46  }