Skip to content

Commit

Permalink
Merge pull request moby#23290 from yongtang/23211-spf13-cobra-rename
Browse files Browse the repository at this point in the history
Use spf13/cobra for docker rename
  • Loading branch information
vdemeester committed Jun 7, 2016
2 parents 3d8fdbb + 70f7ccb commit e83dad0
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 38 deletions.
1 change: 0 additions & 1 deletion api/client/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,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,
Expand Down
53 changes: 53 additions & 0 deletions api/client/container/rename.go
Original file line number Diff line number Diff line change
@@ -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
}
34 changes: 0 additions & 34 deletions api/client/rename.go

This file was deleted.

1 change: 1 addition & 0 deletions cli/cobraadaptor/adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,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),
Expand Down
1 change: 0 additions & 1 deletion cli/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,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"},
Expand Down
4 changes: 2 additions & 2 deletions integration-cli/docker_cli_rename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit e83dad0

Please sign in to comment.