Parcourir la source

Use spf13/cobra for docker wait

This fix is part of the effort to convert commands to spf13/cobra #23211.

Thif fix coverted command `docker wait` to use spf13/cobra

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
Yong Tang il y a 9 ans
Parent
commit
82f84a67d6
5 fichiers modifiés avec 53 ajouts et 41 suppressions
  1. 0 1
      api/client/commands.go
  2. 52 0
      api/client/container/wait.go
  3. 0 39
      api/client/wait.go
  4. 1 0
      cli/cobraadaptor/adaptor.go
  5. 0 1
      cli/usage.go

+ 0 - 1
api/client/commands.go

@@ -32,6 +32,5 @@ func (cli *DockerCli) Command(name string) func(...string) error {
 		"top":     cli.CmdTop,
 		"top":     cli.CmdTop,
 		"update":  cli.CmdUpdate,
 		"update":  cli.CmdUpdate,
 		"version": cli.CmdVersion,
 		"version": cli.CmdVersion,
-		"wait":    cli.CmdWait,
 	}[name]
 	}[name]
 }
 }

+ 52 - 0
api/client/container/wait.go

@@ -0,0 +1,52 @@
+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 waitOptions struct {
+	containers []string
+}
+
+// NewWaitCommand creats a new cobra.Command for `docker wait`
+func NewWaitCommand(dockerCli *client.DockerCli) *cobra.Command {
+	var opts waitOptions
+
+	cmd := &cobra.Command{
+		Use:   "wait CONTAINER [CONTAINER...]",
+		Short: "Block until a container stops, then print its exit code",
+		Args:  cli.RequiresMinArgs(1),
+		RunE: func(cmd *cobra.Command, args []string) error {
+			opts.containers = args
+			return runWait(dockerCli, &opts)
+		},
+	}
+	cmd.SetFlagErrorFunc(flagErrorFunc)
+
+	return cmd
+}
+
+func runWait(dockerCli *client.DockerCli, opts *waitOptions) error {
+	ctx := context.Background()
+
+	var errs []string
+	for _, container := range opts.containers {
+		status, err := dockerCli.Client().ContainerWait(ctx, container)
+		if err != nil {
+			errs = append(errs, err.Error())
+		} else {
+			fmt.Fprintf(dockerCli.Out(), "%d\n", status)
+		}
+	}
+	if len(errs) > 0 {
+		return fmt.Errorf("%s", strings.Join(errs, "\n"))
+	}
+	return nil
+}

+ 0 - 39
api/client/wait.go

@@ -1,39 +0,0 @@
-package client
-
-import (
-	"fmt"
-	"strings"
-
-	"golang.org/x/net/context"
-
-	Cli "github.com/docker/docker/cli"
-	flag "github.com/docker/docker/pkg/mflag"
-)
-
-// CmdWait blocks until a container stops, then prints its exit code.
-//
-// If more than one container is specified, this will wait synchronously on each container.
-//
-// Usage: docker wait CONTAINER [CONTAINER...]
-func (cli *DockerCli) CmdWait(args ...string) error {
-	cmd := Cli.Subcmd("wait", []string{"CONTAINER [CONTAINER...]"}, Cli.DockerCommands["wait"].Description, true)
-	cmd.Require(flag.Min, 1)
-
-	cmd.ParseFlags(args, true)
-
-	ctx := context.Background()
-
-	var errs []string
-	for _, name := range cmd.Args() {
-		status, err := cli.client.ContainerWait(ctx, name)
-		if err != nil {
-			errs = append(errs, err.Error())
-		} else {
-			fmt.Fprintf(cli.out, "%d\n", status)
-		}
-	}
-	if len(errs) > 0 {
-		return fmt.Errorf("%s", strings.Join(errs, "\n"))
-	}
-	return nil
-}

+ 1 - 0
cli/cobraadaptor/adaptor.go

@@ -42,6 +42,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor {
 		container.NewStartCommand(dockerCli),
 		container.NewStartCommand(dockerCli),
 		container.NewStopCommand(dockerCli),
 		container.NewStopCommand(dockerCli),
 		container.NewUnpauseCommand(dockerCli),
 		container.NewUnpauseCommand(dockerCli),
+		container.NewWaitCommand(dockerCli),
 		image.NewRemoveCommand(dockerCli),
 		image.NewRemoveCommand(dockerCli),
 		image.NewSearchCommand(dockerCli),
 		image.NewSearchCommand(dockerCli),
 		network.NewNetworkCommand(dockerCli),
 		network.NewNetworkCommand(dockerCli),

+ 0 - 1
cli/usage.go

@@ -37,7 +37,6 @@ var DockerCommandUsage = []Command{
 	{"top", "Display the running processes of a container"},
 	{"top", "Display the running processes of a container"},
 	{"update", "Update configuration of one or more containers"},
 	{"update", "Update configuration of one or more containers"},
 	{"version", "Show the Docker version information"},
 	{"version", "Show the Docker version information"},
-	{"wait", "Block until a container stops, then print its exit code"},
 }
 }
 
 
 // DockerCommands stores all the docker command
 // DockerCommands stores all the docker command