2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
|
2015-05-05 00:18:28 -04:00
|
|
|
Cli "github.com/docker/docker/cli"
|
2015-03-24 23:57:23 -04:00
|
|
|
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 {
|
2015-10-08 08:46:21 -04:00
|
|
|
cmd := Cli.Subcmd("save", []string{"IMAGE [IMAGE...]"}, Cli.DockerCommands["save"].Description+" (streamed to STDOUT by default)", true)
|
2015-09-02 04:43:49 -04:00
|
|
|
outfile := cmd.String([]string{"o", "-output"}, "", "Write to a file, instead of STDOUT")
|
2015-03-24 23:57:23 -04:00
|
|
|
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 (
|
2015-07-20 20:55:30 -04:00
|
|
|
output = cli.out
|
2015-03-24 23:57:23 -04:00
|
|
|
err error
|
|
|
|
)
|
2015-09-10 08:28:38 -04:00
|
|
|
|
|
|
|
if *outfile == "" && cli.isTerminalOut {
|
|
|
|
return errors.New("Cowardly refusing to save to a terminal. Use the -o flag or redirect.")
|
|
|
|
}
|
2015-03-24 23:57:23 -04:00
|
|
|
if *outfile != "" {
|
2015-09-10 08:28:38 -04:00
|
|
|
if output, err = os.Create(*outfile); err != nil {
|
2015-03-24 23:57:23 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-01 14:23:44 -04:00
|
|
|
sopts := &streamOpts{
|
|
|
|
rawTerminal: true,
|
|
|
|
out: output,
|
|
|
|
}
|
|
|
|
|
2015-09-10 08:28:38 -04:00
|
|
|
v := url.Values{}
|
|
|
|
for _, arg := range cmd.Args() {
|
|
|
|
v.Add("names", arg)
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
2015-09-10 08:28:38 -04:00
|
|
|
if _, err := cli.stream("GET", "/images/get?"+v.Encode(), sopts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-24 23:57:23 -04:00
|
|
|
return nil
|
|
|
|
}
|