wait.go 1.1 KB

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