diff --git a/api/client/commands.go b/api/client/commands.go index a1ae5cd5a8..bf49939b5b 100644 --- a/api/client/commands.go +++ b/api/client/commands.go @@ -32,6 +32,5 @@ func (cli *DockerCli) Command(name string) func(...string) error { "top": cli.CmdTop, "update": cli.CmdUpdate, "version": cli.CmdVersion, - "wait": cli.CmdWait, }[name] } diff --git a/api/client/container/wait.go b/api/client/container/wait.go new file mode 100644 index 0000000000..e92bf6238c --- /dev/null +++ b/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 +} diff --git a/api/client/wait.go b/api/client/wait.go deleted file mode 100644 index 02a65d8660..0000000000 --- a/api/client/wait.go +++ /dev/null @@ -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 -} diff --git a/cli/cobraadaptor/adaptor.go b/cli/cobraadaptor/adaptor.go index 4ceb57757d..ee51435b87 100644 --- a/cli/cobraadaptor/adaptor.go +++ b/cli/cobraadaptor/adaptor.go @@ -42,6 +42,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor { container.NewStartCommand(dockerCli), container.NewStopCommand(dockerCli), container.NewUnpauseCommand(dockerCli), + container.NewWaitCommand(dockerCli), image.NewRemoveCommand(dockerCli), image.NewSearchCommand(dockerCli), network.NewNetworkCommand(dockerCli), diff --git a/cli/usage.go b/cli/usage.go index f6152e5c93..23c357e1cf 100644 --- a/cli/usage.go +++ b/cli/usage.go @@ -37,7 +37,6 @@ var DockerCommandUsage = []Command{ {"top", "Display the running processes of a container"}, {"update", "Update configuration of one or more containers"}, {"version", "Show the Docker version information"}, - {"wait", "Block until a container stops, then print its exit code"}, } // DockerCommands stores all the docker command