2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
|
|
|
)
|
|
|
|
|
2015-03-25 13:34:41 -04:00
|
|
|
// CmdSave saves one or more images to a tar archive.
|
|
|
|
//
|
|
|
|
// The tar archive is written to STDOUT by default, or written to a file.
|
|
|
|
//
|
|
|
|
// Usage: docker save [OPTIONS] IMAGE [IMAGE...]
|
2015-03-24 23:57:23 -04:00
|
|
|
func (cli *DockerCli) CmdSave(args ...string) error {
|
|
|
|
cmd := cli.Subcmd("save", "IMAGE [IMAGE...]", "Save an image(s) to a tar archive (streamed to STDOUT by default)", true)
|
|
|
|
outfile := cmd.String([]string{"o", "-output"}, "", "Write to an file, instead of STDOUT")
|
|
|
|
cmd.Require(flag.Min, 1)
|
|
|
|
|
2015-03-28 21:22:46 -04:00
|
|
|
cmd.ParseFlags(args, true)
|
2015-03-24 23:57:23 -04:00
|
|
|
|
|
|
|
var (
|
|
|
|
output io.Writer = cli.out
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if *outfile != "" {
|
|
|
|
output, err = os.Create(*outfile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if cli.isTerminalOut {
|
|
|
|
return errors.New("Cowardly refusing to save to a terminal. Use the -o flag or redirect.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cmd.Args()) == 1 {
|
|
|
|
image := cmd.Arg(0)
|
|
|
|
if err := cli.stream("GET", "/images/"+image+"/get", nil, output, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
v := url.Values{}
|
|
|
|
for _, arg := range cmd.Args() {
|
|
|
|
v.Add("names", arg)
|
|
|
|
}
|
|
|
|
if err := cli.stream("GET", "/images/get?"+v.Encode(), nil, output, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|