github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/api/client/save.go (about) 1 package client 2 3 import ( 4 "errors" 5 "io" 6 7 Cli "github.com/docker/docker/cli" 8 flag "github.com/docker/docker/pkg/mflag" 9 ) 10 11 // CmdSave saves one or more images to a tar archive. 12 // 13 // The tar archive is written to STDOUT by default, or written to a file. 14 // 15 // Usage: docker save [OPTIONS] IMAGE [IMAGE...] 16 func (cli *DockerCli) CmdSave(args ...string) error { 17 cmd := Cli.Subcmd("save", []string{"IMAGE [IMAGE...]"}, Cli.DockerCommands["save"].Description+" (streamed to STDOUT by default)", true) 18 outfile := cmd.String([]string{"o", "-output"}, "", "Write to a file, instead of STDOUT") 19 cmd.Require(flag.Min, 1) 20 21 cmd.ParseFlags(args, true) 22 23 if *outfile == "" && cli.isTerminalOut { 24 return errors.New("Cowardly refusing to save to a terminal. Use the -o flag or redirect.") 25 } 26 27 responseBody, err := cli.client.ImageSave(cmd.Args()) 28 if err != nil { 29 return err 30 } 31 defer responseBody.Close() 32 33 if *outfile == "" { 34 _, err := io.Copy(cli.out, responseBody) 35 return err 36 } 37 38 return copyToFile(*outfile, responseBody) 39 40 }