mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
7b7ea8ab81
Signed-off-by: Daniel Nephin <dnephin@docker.com>
51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package container
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/docker/docker/cli"
|
|
"github.com/docker/docker/cli/command"
|
|
"github.com/pkg/errors"
|
|
"github.com/spf13/cobra"
|
|
"golang.org/x/net/context"
|
|
)
|
|
|
|
type renameOptions struct {
|
|
oldName string
|
|
newName string
|
|
}
|
|
|
|
// NewRenameCommand creates a new cobra.Command for `docker rename`
|
|
func NewRenameCommand(dockerCli *command.DockerCli) *cobra.Command {
|
|
var opts renameOptions
|
|
|
|
cmd := &cobra.Command{
|
|
Use: "rename CONTAINER 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)
|
|
},
|
|
}
|
|
return cmd
|
|
}
|
|
|
|
func runRename(dockerCli *command.DockerCli, opts *renameOptions) error {
|
|
ctx := context.Background()
|
|
|
|
oldName := strings.TrimSpace(opts.oldName)
|
|
newName := strings.TrimSpace(opts.newName)
|
|
|
|
if oldName == "" || newName == "" {
|
|
return errors.New("Error: Neither old nor new names may be empty")
|
|
}
|
|
|
|
if err := dockerCli.Client().ContainerRename(ctx, oldName, newName); err != nil {
|
|
fmt.Fprintln(dockerCli.Err(), err)
|
|
return errors.Errorf("Error: failed to rename container named %s", oldName)
|
|
}
|
|
return nil
|
|
}
|