github.com/torfuzx/docker@v1.8.1/api/client/save.go (about)

     1  package client
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  	"net/url"
     7  	"os"
     8  
     9  	Cli "github.com/docker/docker/cli"
    10  	flag "github.com/docker/docker/pkg/mflag"
    11  )
    12  
    13  // CmdSave saves one or more images to a tar archive.
    14  //
    15  // The tar archive is written to STDOUT by default, or written to a file.
    16  //
    17  // Usage: docker save [OPTIONS] IMAGE [IMAGE...]
    18  func (cli *DockerCli) CmdSave(args ...string) error {
    19  	cmd := Cli.Subcmd("save", []string{"IMAGE [IMAGE...]"}, "Save an image(s) to a tar archive (streamed to STDOUT by default)", true)
    20  	outfile := cmd.String([]string{"o", "-output"}, "", "Write to an file, instead of STDOUT")
    21  	cmd.Require(flag.Min, 1)
    22  
    23  	cmd.ParseFlags(args, true)
    24  
    25  	var (
    26  		output io.Writer = cli.out
    27  		err    error
    28  	)
    29  	if *outfile != "" {
    30  		output, err = os.Create(*outfile)
    31  		if err != nil {
    32  			return err
    33  		}
    34  	} else if cli.isTerminalOut {
    35  		return errors.New("Cowardly refusing to save to a terminal. Use the -o flag or redirect.")
    36  	}
    37  
    38  	sopts := &streamOpts{
    39  		rawTerminal: true,
    40  		out:         output,
    41  	}
    42  
    43  	if len(cmd.Args()) == 1 {
    44  		image := cmd.Arg(0)
    45  		if _, err := cli.stream("GET", "/images/"+image+"/get", sopts); err != nil {
    46  			return err
    47  		}
    48  	} else {
    49  		v := url.Values{}
    50  		for _, arg := range cmd.Args() {
    51  			v.Add("names", arg)
    52  		}
    53  		if _, err := cli.stream("GET", "/images/get?"+v.Encode(), sopts); err != nil {
    54  			return err
    55  		}
    56  	}
    57  	return nil
    58  }