2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2015-12-03 14:49:41 -05:00
|
|
|
"io"
|
2015-03-24 23:57:23 -04:00
|
|
|
"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
|
|
|
// CmdExport exports a filesystem as a tar archive.
|
|
|
|
//
|
|
|
|
// The tar archive is streamed to STDOUT by default or written to a file.
|
|
|
|
//
|
|
|
|
// Usage: docker export [OPTIONS] CONTAINER
|
2015-03-24 23:57:23 -04:00
|
|
|
func (cli *DockerCli) CmdExport(args ...string) error {
|
2015-10-08 08:46:21 -04:00
|
|
|
cmd := Cli.Subcmd("export", []string{"CONTAINER"}, Cli.DockerCommands["export"].Description, true)
|
2015-03-24 23:57:23 -04:00
|
|
|
outfile := cmd.String([]string{"o", "-output"}, "", "Write to a file, instead of STDOUT")
|
|
|
|
cmd.Require(flag.Exact, 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
|
|
|
|
)
|
|
|
|
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.")
|
|
|
|
}
|
|
|
|
|
2015-12-03 14:49:41 -05:00
|
|
|
responseBody, err := cli.client.ContainerExport(cmd.Arg(0))
|
|
|
|
if err != nil {
|
2015-04-27 00:41:03 -04:00
|
|
|
return err
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
2015-12-03 14:49:41 -05:00
|
|
|
defer responseBody.Close()
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-12-03 14:49:41 -05:00
|
|
|
_, err = io.Copy(output, responseBody)
|
|
|
|
return err
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|