github.com/a4a881d4/docker@v1.9.0-rc2/api/client/save.go (about)

     1  package client
     2  
     3  import (
     4  	"errors"
     5  	"net/url"
     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  	sopts := &streamOpts{
    39  		rawTerminal: true,
    40  		out:         output,
    41  	}
    42  
    43  	v := url.Values{}
    44  	for _, arg := range cmd.Args() {
    45  		v.Add("names", arg)
    46  	}
    47  	if _, err := cli.stream("GET", "/images/get?"+v.Encode(), sopts); err != nil {
    48  		return err
    49  	}
    50  
    51  	return nil
    52  }