wait.go 1.1 KB

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