github.com/flavio/docker@v0.1.3-0.20170117145210-f63d1a6eec47/cli/command/container/wait.go (about)

     1  package container
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/docker/docker/cli"
     9  	"github.com/docker/docker/cli/command"
    10  	"github.com/spf13/cobra"
    11  	"golang.org/x/net/context"
    12  )
    13  
    14  type waitOptions struct {
    15  	containers []string
    16  }
    17  
    18  // NewWaitCommand creates a new cobra.Command for `docker wait`
    19  func NewWaitCommand(dockerCli *command.DockerCli) *cobra.Command {
    20  	var opts waitOptions
    21  
    22  	cmd := &cobra.Command{
    23  		Use:   "wait CONTAINER [CONTAINER...]",
    24  		Short: "Block until one or more containers stop, then print their exit codes",
    25  		Args:  cli.RequiresMinArgs(1),
    26  		RunE: func(cmd *cobra.Command, args []string) error {
    27  			opts.containers = args
    28  			return runWait(dockerCli, &opts)
    29  		},
    30  	}
    31  	return cmd
    32  }
    33  
    34  func runWait(dockerCli *command.DockerCli, opts *waitOptions) error {
    35  	ctx := context.Background()
    36  
    37  	var errs []string
    38  	for _, container := range opts.containers {
    39  		status, err := dockerCli.Client().ContainerWait(ctx, container)
    40  		if err != nil {
    41  			errs = append(errs, err.Error())
    42  			continue
    43  		}
    44  		fmt.Fprintf(dockerCli.Out(), "%d\n", status)
    45  	}
    46  	if len(errs) > 0 {
    47  		return errors.New(strings.Join(errs, "\n"))
    48  	}
    49  	return nil
    50  }