github.1git.de/docker/cli@v26.1.3+incompatible/cli/command/container/wait.go (about)

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