From 9d2e8abd302ae72e4dd5bddf325eb921a9e73a1b Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Mon, 6 Jun 2016 13:58:23 +0200 Subject: [PATCH] Migrate import command to cobra Signed-off-by: Vincent Demeester --- api/client/commands.go | 1 - api/client/image/import.go | 86 +++++++++++++++++++++++++++++++++++++ api/client/import.go | 68 ----------------------------- cli/cobraadaptor/adaptor.go | 1 + cli/usage.go | 1 - 5 files changed, 87 insertions(+), 70 deletions(-) create mode 100644 api/client/image/import.go delete mode 100644 api/client/import.go diff --git a/api/client/commands.go b/api/client/commands.go index a1ae5cd5a8..3b30e0da00 100644 --- a/api/client/commands.go +++ b/api/client/commands.go @@ -11,7 +11,6 @@ func (cli *DockerCli) Command(name string) func(...string) error { "exec": cli.CmdExec, "history": cli.CmdHistory, "images": cli.CmdImages, - "import": cli.CmdImport, "info": cli.CmdInfo, "inspect": cli.CmdInspect, "kill": cli.CmdKill, diff --git a/api/client/image/import.go b/api/client/image/import.go new file mode 100644 index 0000000000..2a7c37b905 --- /dev/null +++ b/api/client/image/import.go @@ -0,0 +1,86 @@ +package image + +import ( + "io" + "os" + + "golang.org/x/net/context" + + "github.com/docker/docker/api/client" + "github.com/docker/docker/cli" + "github.com/docker/docker/pkg/jsonmessage" + "github.com/docker/docker/pkg/urlutil" + "github.com/docker/engine-api/types" + "github.com/spf13/cobra" +) + +type importOptions struct { + source string + reference string + changes []string + message string +} + +// NewImportCommand creates a new `docker import` command +func NewImportCommand(dockerCli *client.DockerCli) *cobra.Command { + var opts importOptions + + cmd := &cobra.Command{ + Use: "import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]", + Short: "Import the contents from a tarball to create a filesystem image", + Args: cli.RequiresMinArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + opts.source = args[0] + if len(args) > 1 { + opts.reference = args[1] + } + return runImport(dockerCli, opts) + }, + } + + flags := cmd.Flags() + + flags.StringSliceVarP(&opts.changes, "change", "c", []string{}, "Apply Dockerfile instruction to the created image") + flags.StringVarP(&opts.message, "message", "m", "", "Set commit message for imported image") + + return cmd +} + +func runImport(dockerCli *client.DockerCli, opts importOptions) error { + var ( + in io.Reader + srcName = opts.source + ) + + if opts.source == "-" { + in = dockerCli.In() + } else if !urlutil.IsURL(opts.source) { + srcName = "-" + file, err := os.Open(opts.source) + if err != nil { + return err + } + defer file.Close() + in = file + } + + source := types.ImageImportSource{ + Source: in, + SourceName: srcName, + } + + options := types.ImageImportOptions{ + Message: opts.message, + Changes: opts.changes, + } + + clnt := dockerCli.Client() + + responseBody, err := clnt.ImageImport(context.Background(), source, opts.reference, options) + if err != nil { + return err + } + defer responseBody.Close() + + return jsonmessage.DisplayJSONMessagesStream(responseBody, dockerCli.Out(), dockerCli.OutFd(), dockerCli.IsTerminalOut(), nil) +} diff --git a/api/client/import.go b/api/client/import.go deleted file mode 100644 index 4948191049..0000000000 --- a/api/client/import.go +++ /dev/null @@ -1,68 +0,0 @@ -package client - -import ( - "io" - "os" - - "golang.org/x/net/context" - - Cli "github.com/docker/docker/cli" - "github.com/docker/docker/opts" - "github.com/docker/docker/pkg/jsonmessage" - flag "github.com/docker/docker/pkg/mflag" - "github.com/docker/docker/pkg/urlutil" - "github.com/docker/engine-api/types" -) - -// CmdImport creates an empty filesystem image, imports the contents of the tarball into the image, and optionally tags the image. -// -// The URL argument is the address of a tarball (.tar, .tar.gz, .tgz, .bzip, .tar.xz, .txz) file or a path to local file relative to docker client. If the URL is '-', then the tar file is read from STDIN. -// -// Usage: docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]] -func (cli *DockerCli) CmdImport(args ...string) error { - cmd := Cli.Subcmd("import", []string{"file|URL|- [REPOSITORY[:TAG]]"}, Cli.DockerCommands["import"].Description, true) - flChanges := opts.NewListOpts(nil) - cmd.Var(&flChanges, []string{"c", "-change"}, "Apply Dockerfile instruction to the created image") - message := cmd.String([]string{"m", "-message"}, "", "Set commit message for imported image") - cmd.Require(flag.Min, 1) - - cmd.ParseFlags(args, true) - - var ( - in io.Reader - src = cmd.Arg(0) - srcName = src - ref = cmd.Arg(1) - changes = flChanges.GetAll() - ) - - if src == "-" { - in = cli.in - } else if !urlutil.IsURL(src) { - srcName = "-" - file, err := os.Open(src) - if err != nil { - return err - } - defer file.Close() - in = file - } - - source := types.ImageImportSource{ - Source: in, - SourceName: srcName, - } - - options := types.ImageImportOptions{ - Message: *message, - Changes: changes, - } - - responseBody, err := cli.client.ImageImport(context.Background(), source, ref, options) - if err != nil { - return err - } - defer responseBody.Close() - - return jsonmessage.DisplayJSONMessagesStream(responseBody, cli.out, cli.outFd, cli.isTerminalOut, nil) -} diff --git a/cli/cobraadaptor/adaptor.go b/cli/cobraadaptor/adaptor.go index 4ceb57757d..05d726dacd 100644 --- a/cli/cobraadaptor/adaptor.go +++ b/cli/cobraadaptor/adaptor.go @@ -44,6 +44,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor { container.NewUnpauseCommand(dockerCli), image.NewRemoveCommand(dockerCli), image.NewSearchCommand(dockerCli), + image.NewImportCommand(dockerCli), network.NewNetworkCommand(dockerCli), volume.NewVolumeCommand(dockerCli), ) diff --git a/cli/usage.go b/cli/usage.go index f6152e5c93..b60395073c 100644 --- a/cli/usage.go +++ b/cli/usage.go @@ -16,7 +16,6 @@ var DockerCommandUsage = []Command{ {"exec", "Run a command in a running container"}, {"history", "Show the history of an image"}, {"images", "List images"}, - {"import", "Import the contents from a tarball to create a filesystem image"}, {"info", "Display system-wide information"}, {"inspect", "Return low-level information on a container or image"}, {"kill", "Kill a running container"},