mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #23290 from yongtang/23211-spf13-cobra-rename
Use spf13/cobra for docker rename
This commit is contained in:
commit
e83dad090a
6 changed files with 56 additions and 38 deletions
|
@ -21,7 +21,6 @@ func (cli *DockerCli) Command(name string) func(...string) error {
|
||||||
"ps": cli.CmdPs,
|
"ps": cli.CmdPs,
|
||||||
"pull": cli.CmdPull,
|
"pull": cli.CmdPull,
|
||||||
"push": cli.CmdPush,
|
"push": cli.CmdPush,
|
||||||
"rename": cli.CmdRename,
|
|
||||||
"restart": cli.CmdRestart,
|
"restart": cli.CmdRestart,
|
||||||
"rm": cli.CmdRm,
|
"rm": cli.CmdRm,
|
||||||
"save": cli.CmdSave,
|
"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
|
|
||||||
}
|
|
|
@ -40,6 +40,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor {
|
||||||
container.NewExportCommand(dockerCli),
|
container.NewExportCommand(dockerCli),
|
||||||
container.NewLogsCommand(dockerCli),
|
container.NewLogsCommand(dockerCli),
|
||||||
container.NewPortCommand(dockerCli),
|
container.NewPortCommand(dockerCli),
|
||||||
|
container.NewRenameCommand(dockerCli),
|
||||||
container.NewRunCommand(dockerCli),
|
container.NewRunCommand(dockerCli),
|
||||||
container.NewStartCommand(dockerCli),
|
container.NewStartCommand(dockerCli),
|
||||||
container.NewStopCommand(dockerCli),
|
container.NewStopCommand(dockerCli),
|
||||||
|
|
|
@ -26,7 +26,6 @@ var DockerCommandUsage = []Command{
|
||||||
{"ps", "List containers"},
|
{"ps", "List containers"},
|
||||||
{"pull", "Pull an image or a repository from a registry"},
|
{"pull", "Pull an image or a repository from a registry"},
|
||||||
{"push", "Push an image or a repository to a registry"},
|
{"push", "Push an image or a repository to a registry"},
|
||||||
{"rename", "Rename a container"},
|
|
||||||
{"restart", "Restart a container"},
|
{"restart", "Restart a container"},
|
||||||
{"rm", "Remove one or more containers"},
|
{"rm", "Remove one or more containers"},
|
||||||
{"save", "Save one or more images to a tar archive"},
|
{"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", "")
|
out, _, err = dockerCmdWithError("rename", "myname", "")
|
||||||
c.Assert(err, checker.NotNil, check.Commentf("Renaming container to invalid name should have failed: %s", out))
|
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")
|
out, _, err = dockerCmdWithError("rename", "", "newname")
|
||||||
c.Assert(err, checker.NotNil, check.Commentf("Renaming container with empty name should have failed: %s", out))
|
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")
|
out, _ = dockerCmd(c, "ps", "-a")
|
||||||
c.Assert(out, checker.Contains, "myname", check.Commentf("Output of docker ps should have included 'myname': %s", out))
|
c.Assert(out, checker.Contains, "myname", check.Commentf("Output of docker ps should have included 'myname': %s", out))
|
||||||
|
|
Loading…
Reference in a new issue