Browse code

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 <yong.tang.github@outlook.com>

Yong Tang authored on 2016/06/06 11:16:26
Showing 6 changed files
... ...
@@ -22,7 +22,6 @@ func (cli *DockerCli) Command(name string) func(...string) error {
22 22
 		"ps":      cli.CmdPs,
23 23
 		"pull":    cli.CmdPull,
24 24
 		"push":    cli.CmdPush,
25
-		"rename":  cli.CmdRename,
26 25
 		"restart": cli.CmdRestart,
27 26
 		"rm":      cli.CmdRm,
28 27
 		"save":    cli.CmdSave,
29 28
new file mode 100644
... ...
@@ -0,0 +1,53 @@
0
+package container
1
+
2
+import (
3
+	"fmt"
4
+	"strings"
5
+
6
+	"golang.org/x/net/context"
7
+
8
+	"github.com/docker/docker/api/client"
9
+	"github.com/docker/docker/cli"
10
+	"github.com/spf13/cobra"
11
+)
12
+
13
+type renameOptions struct {
14
+	oldName string
15
+	newName string
16
+}
17
+
18
+// NewRenameCommand creats a new cobra.Command for `docker rename`
19
+func NewRenameCommand(dockerCli *client.DockerCli) *cobra.Command {
20
+	var opts renameOptions
21
+
22
+	cmd := &cobra.Command{
23
+		Use:   "rename OLD_NAME NEW_NAME",
24
+		Short: "Rename a container",
25
+		Args:  cli.ExactArgs(2),
26
+		RunE: func(cmd *cobra.Command, args []string) error {
27
+			opts.oldName = args[0]
28
+			opts.newName = args[1]
29
+			return runRename(dockerCli, &opts)
30
+		},
31
+	}
32
+	cmd.SetFlagErrorFunc(flagErrorFunc)
33
+
34
+	return cmd
35
+}
36
+
37
+func runRename(dockerCli *client.DockerCli, opts *renameOptions) error {
38
+	ctx := context.Background()
39
+
40
+	oldName := strings.TrimSpace(opts.oldName)
41
+	newName := strings.TrimSpace(opts.newName)
42
+
43
+	if oldName == "" || newName == "" {
44
+		return fmt.Errorf("Error: Neither old nor new names may be empty")
45
+	}
46
+
47
+	if err := dockerCli.Client().ContainerRename(ctx, oldName, newName); err != nil {
48
+		fmt.Fprintf(dockerCli.Err(), "%s\n", err)
49
+		return fmt.Errorf("Error: failed to rename container named %s", oldName)
50
+	}
51
+	return nil
52
+}
0 53
deleted file mode 100644
... ...
@@ -1,34 +0,0 @@
1
-package client
2
-
3
-import (
4
-	"fmt"
5
-	"strings"
6
-
7
-	"golang.org/x/net/context"
8
-
9
-	Cli "github.com/docker/docker/cli"
10
-	flag "github.com/docker/docker/pkg/mflag"
11
-)
12
-
13
-// CmdRename renames a container.
14
-//
15
-// Usage: docker rename OLD_NAME NEW_NAME
16
-func (cli *DockerCli) CmdRename(args ...string) error {
17
-	cmd := Cli.Subcmd("rename", []string{"OLD_NAME NEW_NAME"}, Cli.DockerCommands["rename"].Description, true)
18
-	cmd.Require(flag.Exact, 2)
19
-
20
-	cmd.ParseFlags(args, true)
21
-
22
-	oldName := strings.TrimSpace(cmd.Arg(0))
23
-	newName := strings.TrimSpace(cmd.Arg(1))
24
-
25
-	if oldName == "" || newName == "" {
26
-		return fmt.Errorf("Error: Neither old nor new names may be empty")
27
-	}
28
-
29
-	if err := cli.client.ContainerRename(context.Background(), oldName, newName); err != nil {
30
-		fmt.Fprintf(cli.err, "%s\n", err)
31
-		return fmt.Errorf("Error: failed to rename container named %s", oldName)
32
-	}
33
-	return nil
34
-}
... ...
@@ -39,6 +39,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor {
39 39
 		container.NewExportCommand(dockerCli),
40 40
 		container.NewLogsCommand(dockerCli),
41 41
 		container.NewPortCommand(dockerCli),
42
+		container.NewRenameCommand(dockerCli),
42 43
 		container.NewRunCommand(dockerCli),
43 44
 		container.NewStartCommand(dockerCli),
44 45
 		container.NewStopCommand(dockerCli),
... ...
@@ -27,7 +27,6 @@ var DockerCommandUsage = []Command{
27 27
 	{"ps", "List containers"},
28 28
 	{"pull", "Pull an image or a repository from a registry"},
29 29
 	{"push", "Push an image or a repository to a registry"},
30
-	{"rename", "Rename a container"},
31 30
 	{"restart", "Restart a container"},
32 31
 	{"rm", "Remove one or more containers"},
33 32
 	{"save", "Save one or more images to a tar archive"},
... ...
@@ -75,11 +75,11 @@ func (s *DockerSuite) TestRenameInvalidName(c *check.C) {
75 75
 
76 76
 	out, _, err = dockerCmdWithError("rename", "myname", "")
77 77
 	c.Assert(err, checker.NotNil, check.Commentf("Renaming container to invalid name should have failed: %s", out))
78
-	c.Assert(out, checker.Contains, "may be empty", check.Commentf("%v", err))
78
+	c.Assert(out, checker.Contains, "\"docker rename\" requires exactly 2 argument(s).", check.Commentf("%v", err))
79 79
 
80 80
 	out, _, err = dockerCmdWithError("rename", "", "newname")
81 81
 	c.Assert(err, checker.NotNil, check.Commentf("Renaming container with empty name should have failed: %s", out))
82
-	c.Assert(out, checker.Contains, "may be empty", check.Commentf("%v", err))
82
+	c.Assert(out, checker.Contains, "\"docker rename\" requires exactly 2 argument(s).", check.Commentf("%v", err))
83 83
 
84 84
 	out, _ = dockerCmd(c, "ps", "-a")
85 85
 	c.Assert(out, checker.Contains, "myname", check.Commentf("Output of docker ps should have included 'myname': %s", out))