mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #23267 from vdemeester/migrate-export-to-cobra
Use spf13/cobra for docker export
This commit is contained in:
commit
c764234c3b
7 changed files with 63 additions and 45 deletions
|
@ -10,7 +10,6 @@ func (cli *DockerCli) Command(name string) func(...string) error {
|
||||||
"diff": cli.CmdDiff,
|
"diff": cli.CmdDiff,
|
||||||
"events": cli.CmdEvents,
|
"events": cli.CmdEvents,
|
||||||
"exec": cli.CmdExec,
|
"exec": cli.CmdExec,
|
||||||
"export": cli.CmdExport,
|
|
||||||
"history": cli.CmdHistory,
|
"history": cli.CmdHistory,
|
||||||
"images": cli.CmdImages,
|
"images": cli.CmdImages,
|
||||||
"import": cli.CmdImport,
|
"import": cli.CmdImport,
|
||||||
|
|
59
api/client/container/export.go
Normal file
59
api/client/container/export.go
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
package container
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
|
"github.com/docker/docker/api/client"
|
||||||
|
"github.com/docker/docker/cli"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
type exportOptions struct {
|
||||||
|
container string
|
||||||
|
output string
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewExportCommand creates a new `docker export` command
|
||||||
|
func NewExportCommand(dockerCli *client.DockerCli) *cobra.Command {
|
||||||
|
var opts exportOptions
|
||||||
|
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "export [OPTIONS] CONTAINER",
|
||||||
|
Short: "Export a container's filesystem as a tar archive",
|
||||||
|
Args: cli.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
opts.container = args[0]
|
||||||
|
return runExport(dockerCli, opts)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
flags := cmd.Flags()
|
||||||
|
|
||||||
|
flags.StringVarP(&opts.output, "output", "o", "", "Write to a file, instead of STDOUT")
|
||||||
|
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
func runExport(dockerCli *client.DockerCli, opts exportOptions) error {
|
||||||
|
if opts.output == "" && dockerCli.IsTerminalOut() {
|
||||||
|
return errors.New("Cowardly refusing to save to a terminal. Use the -o flag or redirect.")
|
||||||
|
}
|
||||||
|
|
||||||
|
clnt := dockerCli.Client()
|
||||||
|
|
||||||
|
responseBody, err := clnt.ContainerExport(context.Background(), opts.container)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer responseBody.Close()
|
||||||
|
|
||||||
|
if opts.output == "" {
|
||||||
|
_, err := io.Copy(dockerCli.Out(), responseBody)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.CopyToFile(opts.output, responseBody)
|
||||||
|
}
|
|
@ -1,41 +0,0 @@
|
||||||
package client
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"golang.org/x/net/context"
|
|
||||||
|
|
||||||
Cli "github.com/docker/docker/cli"
|
|
||||||
flag "github.com/docker/docker/pkg/mflag"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 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
|
|
||||||
func (cli *DockerCli) CmdExport(args ...string) error {
|
|
||||||
cmd := Cli.Subcmd("export", []string{"CONTAINER"}, Cli.DockerCommands["export"].Description, true)
|
|
||||||
outfile := cmd.String([]string{"o", "-output"}, "", "Write to a file, instead of STDOUT")
|
|
||||||
cmd.Require(flag.Exact, 1)
|
|
||||||
|
|
||||||
cmd.ParseFlags(args, true)
|
|
||||||
|
|
||||||
if *outfile == "" && cli.isTerminalOut {
|
|
||||||
return errors.New("Cowardly refusing to save to a terminal. Use the -o flag or redirect.")
|
|
||||||
}
|
|
||||||
|
|
||||||
responseBody, err := cli.client.ContainerExport(context.Background(), cmd.Arg(0))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer responseBody.Close()
|
|
||||||
|
|
||||||
if *outfile == "" {
|
|
||||||
_, err := io.Copy(cli.out, responseBody)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return copyToFile(*outfile, responseBody)
|
|
||||||
}
|
|
|
@ -37,6 +37,6 @@ func (cli *DockerCli) CmdSave(args ...string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return copyToFile(*outfile, responseBody)
|
return CopyToFile(*outfile, responseBody)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -162,7 +162,8 @@ func (cli *DockerCli) GetTtySize() (int, int) {
|
||||||
return int(ws.Height), int(ws.Width)
|
return int(ws.Height), int(ws.Width)
|
||||||
}
|
}
|
||||||
|
|
||||||
func copyToFile(outfile string, r io.Reader) error {
|
// CopyToFile writes the content of the reader to the specifed file
|
||||||
|
func CopyToFile(outfile string, r io.Reader) error {
|
||||||
tmpFile, err := ioutil.TempFile(filepath.Dir(outfile), ".docker_temp_")
|
tmpFile, err := ioutil.TempFile(filepath.Dir(outfile), ".docker_temp_")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -34,6 +34,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor {
|
||||||
rootCmd.SetOutput(stdout)
|
rootCmd.SetOutput(stdout)
|
||||||
rootCmd.AddCommand(
|
rootCmd.AddCommand(
|
||||||
container.NewCreateCommand(dockerCli),
|
container.NewCreateCommand(dockerCli),
|
||||||
|
container.NewExportCommand(dockerCli),
|
||||||
container.NewRunCommand(dockerCli),
|
container.NewRunCommand(dockerCli),
|
||||||
image.NewSearchCommand(dockerCli),
|
image.NewSearchCommand(dockerCli),
|
||||||
volume.NewVolumeCommand(dockerCli),
|
volume.NewVolumeCommand(dockerCli),
|
||||||
|
|
|
@ -15,7 +15,6 @@ var DockerCommandUsage = []Command{
|
||||||
{"diff", "Inspect changes on a container's filesystem"},
|
{"diff", "Inspect changes on a container's filesystem"},
|
||||||
{"events", "Get real time events from the server"},
|
{"events", "Get real time events from the server"},
|
||||||
{"exec", "Run a command in a running container"},
|
{"exec", "Run a command in a running container"},
|
||||||
{"export", "Export a container's filesystem as a tar archive"},
|
|
||||||
{"history", "Show the history of an image"},
|
{"history", "Show the history of an image"},
|
||||||
{"images", "List images"},
|
{"images", "List images"},
|
||||||
{"import", "Import the contents from a tarball to create a filesystem image"},
|
{"import", "Import the contents from a tarball to create a filesystem image"},
|
||||||
|
|
Loading…
Add table
Reference in a new issue