github.com/squaremo/docker@v1.3.2-0.20150516120342-42cfc9554972/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  	sopts := &streamOpts{
    38  		rawTerminal: true,
    39  		out:         output,
    40  	}
    41  
    42  	if len(cmd.Args()) == 1 {
    43  		image := cmd.Arg(0)
    44  		if err := cli.stream("GET", "/images/"+image+"/get", sopts); err != nil {
    45  			return err
    46  		}
    47  	} else {
    48  		v := url.Values{}
    49  		for _, arg := range cmd.Args() {
    50  			v.Add("names", arg)
    51  		}
    52  		if err := cli.stream("GET", "/images/get?"+v.Encode(), sopts); err != nil {
    53  			return err
    54  		}
    55  	}
    56  	return nil
    57  }