From 70f7ccb304f9246bc9c2bfe0e30d3a2bd8fd42f8 Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Sun, 5 Jun 2016 19:16:26 -0700 Subject: [PATCH] Use spf13/cobra for docker rename This fix is part of the effort to convert commands to spf13/cobra #23211. Thif fix coverted command `docker rename` to use spf13/cobra Signed-off-by: Yong Tang --- api/client/commands.go | 1 - api/client/container/rename.go | 53 +++++++++++++++++++++++ api/client/rename.go | 34 --------------- cli/cobraadaptor/adaptor.go | 1 + cli/usage.go | 1 - integration-cli/docker_cli_rename_test.go | 4 +- 6 files changed, 56 insertions(+), 38 deletions(-) create mode 100644 api/client/container/rename.go delete mode 100644 api/client/rename.go diff --git a/api/client/commands.go b/api/client/commands.go index 2f73a6bd57f2d..3bb4b936780d8 100644 --- a/api/client/commands.go +++ b/api/client/commands.go @@ -22,7 +22,6 @@ func (cli *DockerCli) Command(name string) func(...string) error { "ps": cli.CmdPs, "pull": cli.CmdPull, "push": cli.CmdPush, - "rename": cli.CmdRename, "restart": cli.CmdRestart, "rm": cli.CmdRm, "save": cli.CmdSave, diff --git a/api/client/container/rename.go b/api/client/container/rename.go new file mode 100644 index 0000000000000..e6bc723c81b73 --- /dev/null +++ b/api/client/container/rename.go @@ -0,0 +1,53 @@ +package container + +import ( + "fmt" + "strings" + + "golang.org/x/net/context" + + "github.com/docker/docker/api/client" + "github.com/docker/docker/cli" + "github.com/spf13/cobra" +) + +type renameOptions struct { + oldName string + newName string +} + +// NewRenameCommand creats a new cobra.Command for `docker rename` +func NewRenameCommand(dockerCli *client.DockerCli) *cobra.Command { + var opts renameOptions + + cmd := &cobra.Command{ + Use: "rename OLD_NAME NEW_NAME", + Short: "Rename a container", + Args: cli.ExactArgs(2), + RunE: func(cmd *cobra.Command, args []string) error { + opts.oldName = args[0] + opts.newName = args[1] + return runRename(dockerCli, &opts) + }, + } + cmd.SetFlagErrorFunc(flagErrorFunc) + + return cmd +} + +func runRename(dockerCli *client.DockerCli, opts *renameOptions) error { + ctx := context.Background() + + oldName := strings.TrimSpace(opts.oldName) + newName := strings.TrimSpace(opts.newName) + + if oldName == "" || newName == "" { + return fmt.Errorf("Error: Neither old nor new names may be empty") + } + + if err := dockerCli.Client().ContainerRename(ctx, oldName, newName); err != nil { + fmt.Fprintf(dockerCli.Err(), "%s\n", err) + return fmt.Errorf("Error: failed to rename container named %s", oldName) + } + return nil +} diff --git a/api/client/rename.go b/api/client/rename.go deleted file mode 100644 index 6836988167dea..0000000000000 --- a/api/client/rename.go +++ /dev/null @@ -1,34 +0,0 @@ -package client - -import ( - "fmt" - "strings" - - "golang.org/x/net/context" - - Cli "github.com/docker/docker/cli" - flag "github.com/docker/docker/pkg/mflag" -) - -// CmdRename renames a container. -// -// Usage: docker rename OLD_NAME NEW_NAME -func (cli *DockerCli) CmdRename(args ...string) error { - cmd := Cli.Subcmd("rename", []string{"OLD_NAME NEW_NAME"}, Cli.DockerCommands["rename"].Description, true) - cmd.Require(flag.Exact, 2) - - cmd.ParseFlags(args, true) - - oldName := strings.TrimSpace(cmd.Arg(0)) - newName := strings.TrimSpace(cmd.Arg(1)) - - if oldName == "" || newName == "" { - return fmt.Errorf("Error: Neither old nor new names may be empty") - } - - if err := cli.client.ContainerRename(context.Background(), oldName, newName); err != nil { - fmt.Fprintf(cli.err, "%s\n", err) - return fmt.Errorf("Error: failed to rename container named %s", oldName) - } - return nil -} diff --git a/cli/cobraadaptor/adaptor.go b/cli/cobraadaptor/adaptor.go index ca7d4e54c02d9..83b01a3f7fd39 100644 --- a/cli/cobraadaptor/adaptor.go +++ b/cli/cobraadaptor/adaptor.go @@ -39,6 +39,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor { container.NewExportCommand(dockerCli), container.NewLogsCommand(dockerCli), container.NewPortCommand(dockerCli), + container.NewRenameCommand(dockerCli), container.NewRunCommand(dockerCli), container.NewStartCommand(dockerCli), container.NewStopCommand(dockerCli), diff --git a/cli/usage.go b/cli/usage.go index f336d611f29ef..6807951271137 100644 --- a/cli/usage.go +++ b/cli/usage.go @@ -27,7 +27,6 @@ var DockerCommandUsage = []Command{ {"ps", "List containers"}, {"pull", "Pull an image or a repository from a registry"}, {"push", "Push an image or a repository to a registry"}, - {"rename", "Rename a container"}, {"restart", "Restart a container"}, {"rm", "Remove one or more containers"}, {"save", "Save one or more images to a tar archive"}, diff --git a/integration-cli/docker_cli_rename_test.go b/integration-cli/docker_cli_rename_test.go index 98f032c1b7116..375b77606be0a 100644 --- a/integration-cli/docker_cli_rename_test.go +++ b/integration-cli/docker_cli_rename_test.go @@ -75,11 +75,11 @@ func (s *DockerSuite) TestRenameInvalidName(c *check.C) { out, _, err = dockerCmdWithError("rename", "myname", "") c.Assert(err, checker.NotNil, check.Commentf("Renaming container to invalid name should have failed: %s", out)) - c.Assert(out, checker.Contains, "may be empty", check.Commentf("%v", err)) + c.Assert(out, checker.Contains, "\"docker rename\" requires exactly 2 argument(s).", check.Commentf("%v", err)) out, _, err = dockerCmdWithError("rename", "", "newname") c.Assert(err, checker.NotNil, check.Commentf("Renaming container with empty name should have failed: %s", out)) - c.Assert(out, checker.Contains, "may be empty", check.Commentf("%v", err)) + c.Assert(out, checker.Contains, "\"docker rename\" requires exactly 2 argument(s).", check.Commentf("%v", err)) out, _ = dockerCmd(c, "ps", "-a") c.Assert(out, checker.Contains, "myname", check.Commentf("Output of docker ps should have included 'myname': %s", out))