Skip to content

Commit

Permalink
Merge pull request moby#23267 from vdemeester/migrate-export-to-cobra
Browse files Browse the repository at this point in the history
Use spf13/cobra for docker export
  • Loading branch information
thaJeztah committed Jun 5, 2016
2 parents 3cec900 + b25e641 commit c764234
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 45 deletions.
1 change: 0 additions & 1 deletion api/client/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ func (cli *DockerCli) Command(name string) func(...string) error {
"diff": cli.CmdDiff,
"events": cli.CmdEvents,
"exec": cli.CmdExec,
"export": cli.CmdExport,
"history": cli.CmdHistory,
"images": cli.CmdImages,
"import": cli.CmdImport,
Expand Down
59 changes: 59 additions & 0 deletions api/client/container/export.go
Original file line number Diff line number Diff line change
@@ -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)
}
41 changes: 0 additions & 41 deletions api/client/export.go

This file was deleted.

2 changes: 1 addition & 1 deletion api/client/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ func (cli *DockerCli) CmdSave(args ...string) error {
return err
}

return copyToFile(*outfile, responseBody)
return CopyToFile(*outfile, responseBody)

}
3 changes: 2 additions & 1 deletion api/client/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ func (cli *DockerCli) GetTtySize() (int, int) {
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_")
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions cli/cobraadaptor/adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor {
rootCmd.SetOutput(stdout)
rootCmd.AddCommand(
container.NewCreateCommand(dockerCli),
container.NewExportCommand(dockerCli),
container.NewRunCommand(dockerCli),
image.NewSearchCommand(dockerCli),
volume.NewVolumeCommand(dockerCli),
Expand Down
1 change: 0 additions & 1 deletion cli/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ var DockerCommandUsage = []Command{
{"diff", "Inspect changes on a container's filesystem"},
{"events", "Get real time events from the server"},
{"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"},
{"images", "List images"},
{"import", "Import the contents from a tarball to create a filesystem image"},
Expand Down

0 comments on commit c764234

Please sign in to comment.