From c289179c99c9b66f798f19e8bc39152a6d4d0ed3 Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Sun, 5 Jun 2016 16:48:26 -0700 Subject: [PATCH] Use spf13/cobra for docker port This fix is part of the effort to convert commands to spf13/cobra #23211. Thif fix coverted command `docker port` to use spf13/cobra Note: As part of this fix, a new function `RequiresMinMaxArgs(min int, max int)` has been added in cli/required.go. This function restrict the args to be at least min and at most max. Signed-off-by: Yong Tang --- cobraadaptor/adaptor.go | 1 + required.go | 18 ++++++++++++++++++ usage.go | 1 - 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/cobraadaptor/adaptor.go b/cobraadaptor/adaptor.go index 4ceb57757d..b873e9cae9 100644 --- a/cobraadaptor/adaptor.go +++ b/cobraadaptor/adaptor.go @@ -38,6 +38,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor { container.NewDiffCommand(dockerCli), container.NewExportCommand(dockerCli), container.NewLogsCommand(dockerCli), + container.NewPortCommand(dockerCli), container.NewRunCommand(dockerCli), container.NewStartCommand(dockerCli), container.NewStopCommand(dockerCli), diff --git a/required.go b/required.go index 1bbd55f524..0cf35b3d68 100644 --- a/required.go +++ b/required.go @@ -43,6 +43,24 @@ func RequiresMinArgs(min int) cobra.PositionalArgs { } } +// RequiresMinMaxArgs returns an error if there is not at least min args and at most max args +func RequiresMinMaxArgs(min int, max int) cobra.PositionalArgs { + return func(cmd *cobra.Command, args []string) error { + if len(args) >= min && len(args) <= max { + return nil + } + return fmt.Errorf( + "\"%s\" requires at least %d and at most %d argument(s).\nSee '%s --help'.\n\nUsage: %s\n\n%s", + cmd.CommandPath(), + min, + max, + cmd.CommandPath(), + cmd.UseLine(), + cmd.Short, + ) + } +} + // ExactArgs returns an error if there is not the exact number of args func ExactArgs(number int) cobra.PositionalArgs { return func(cmd *cobra.Command, args []string) error { diff --git a/usage.go b/usage.go index f6152e5c93..fc232b5ecc 100644 --- a/usage.go +++ b/usage.go @@ -24,7 +24,6 @@ var DockerCommandUsage = []Command{ {"login", "Log in to a Docker registry"}, {"logout", "Log out from a Docker registry"}, {"pause", "Pause all processes within a container"}, - {"port", "List port mappings or a specific mapping for the CONTAINER"}, {"ps", "List containers"}, {"pull", "Pull an image or a repository from a registry"}, {"push", "Push an image or a repository to a registry"},