-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request moby#23290 from yongtang/23211-spf13-cobra-rename
Use spf13/cobra for docker rename
- Loading branch information
Showing
6 changed files
with
56 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters