2015-03-25 03:57:23 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2015-12-04 18:14:20 +00:00
|
|
|
"io"
|
2015-03-25 03:57:23 +00:00
|
|
|
|
2016-02-03 23:41:26 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2015-05-05 04:18:28 +00:00
|
|
|
Cli "github.com/docker/docker/cli"
|
2015-03-25 03:57:23 +00:00
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
|
|
|
)
|
|
|
|
|
2015-03-25 17:34:41 +00: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-25 03:57:23 +00:00
|
|
|
func (cli *DockerCli) CmdSave(args ...string) error {
|
2015-10-08 12:46:21 +00:00
|
|
|
cmd := Cli.Subcmd("save", []string{"IMAGE [IMAGE...]"}, Cli.DockerCommands["save"].Description+" (streamed to STDOUT by default)", true)
|
2015-09-02 08:43:49 +00:00
|
|
|
outfile := cmd.String([]string{"o", "-output"}, "", "Write to a file, instead of STDOUT")
|
2015-03-25 03:57:23 +00:00
|
|
|
cmd.Require(flag.Min, 1)
|
|
|
|
|
2015-03-29 01:22:46 +00:00
|
|
|
cmd.ParseFlags(args, true)
|
2015-03-25 03:57:23 +00:00
|
|
|
|
2015-09-10 12:28:38 +00:00
|
|
|
if *outfile == "" && cli.isTerminalOut {
|
|
|
|
return errors.New("Cowardly refusing to save to a terminal. Use the -o flag or redirect.")
|
|
|
|
}
|
2015-03-25 03:57:23 +00:00
|
|
|
|
2016-02-03 23:41:26 +00:00
|
|
|
responseBody, err := cli.client.ImageSave(context.Background(), cmd.Args())
|
2015-12-04 18:14:20 +00:00
|
|
|
if err != nil {
|
2015-09-10 12:28:38 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-12-04 18:14:20 +00:00
|
|
|
defer responseBody.Close()
|
2015-09-10 12:28:38 +00:00
|
|
|
|
2015-12-14 01:25:28 +00:00
|
|
|
if *outfile == "" {
|
|
|
|
_, err := io.Copy(cli.out, responseBody)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return copyToFile(*outfile, responseBody)
|
|
|
|
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|