github.com/guilhermebr/docker@v1.4.2-0.20150428121140-67da055cebca/api/client/save.go (about)

     1  package client
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  	"net/url"
     7  	"os"
     8  
     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", "IMAGE [IMAGE...]", "Save an image(s) to a tar archive (streamed to STDOUT by default)", true)
    19  	outfile := cmd.String([]string{"o", "-output"}, "", "Write to an file, instead of STDOUT")
    20  	cmd.Require(flag.Min, 1)
    21  
    22  	cmd.ParseFlags(args, true)
    23  
    24  	var (
    25  		output io.Writer = 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  	if len(cmd.Args()) == 1 {
    38  		image := cmd.Arg(0)
    39  		if err := cli.stream("GET", "/images/"+image+"/get", nil, output, nil); err != nil {
    40  			return err
    41  		}
    42  	} else {
    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(), nil, output, nil); err != nil {
    48  			return err
    49  		}
    50  	}
    51  	return nil
    52  }