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>
This commit is contained in:
parent
3d80884f3d
commit
70f7ccb304
6 changed files with 56 additions and 38 deletions
|
@ -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,
|
||||
|
|
53
api/client/container/rename.go
Normal file
53
api/client/container/rename.go
Normal file
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -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),
|
||||
|
|
|
@ -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"},
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Reference in a new issue