2016-06-05 22:16:26 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
2016-06-05 22:16:26 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type renameOptions struct {
|
|
|
|
oldName string
|
|
|
|
newName string
|
|
|
|
}
|
|
|
|
|
2016-07-16 05:55:18 -04:00
|
|
|
// NewRenameCommand creates a new cobra.Command for `docker rename`
|
2016-09-08 13:11:39 -04:00
|
|
|
func NewRenameCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-06-05 22:16:26 -04:00
|
|
|
var opts renameOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2016-08-31 21:25:11 -04:00
|
|
|
Use: "rename CONTAINER NEW_NAME",
|
2016-06-05 22:16:26 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func runRename(dockerCli *command.DockerCli, opts *renameOptions) error {
|
2016-06-05 22:16:26 -04:00
|
|
|
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
|
|
|
|
}
|