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
|
|
|
|
2016-02-03 18:41:26 -05:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
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
|
|
|
|
2015-12-13 20:25:28 -05:00
|
|
|
if *outfile == "" && cli.isTerminalOut {
|
2015-03-24 23:57:23 -04:00
|
|
|
return errors.New("Cowardly refusing to save to a terminal. Use the -o flag or redirect.")
|
|
|
|
}
|
|
|
|
|
2016-02-03 18:41:26 -05:00
|
|
|
responseBody, err := cli.client.ContainerExport(context.Background(), cmd.Arg(0))
|
2015-12-03 14:49:41 -05:00
|
|
|
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-13 20:25:28 -05:00
|
|
|
if *outfile == "" {
|
|
|
|
_, err := io.Copy(cli.out, responseBody)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return copyToFile(*outfile, responseBody)
|
|
|
|
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|