github.com/portworx/docker@v1.12.1/api/client/container/wait.go (about) 1 package container 2 3 import ( 4 "fmt" 5 "strings" 6 7 "golang.org/x/net/context" 8 9 "github.com/docker/docker/api/client" 10 "github.com/docker/docker/cli" 11 "github.com/spf13/cobra" 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 *client.DockerCli) *cobra.Command { 20 var opts waitOptions 21 22 cmd := &cobra.Command{ 23 Use: "wait CONTAINER [CONTAINER...]", 24 Short: "Block until a container stops, then print its exit code", 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 cmd.SetFlagErrorFunc(flagErrorFunc) 32 33 return cmd 34 } 35 36 func runWait(dockerCli *client.DockerCli, opts *waitOptions) error { 37 ctx := context.Background() 38 39 var errs []string 40 for _, container := range opts.containers { 41 status, err := dockerCli.Client().ContainerWait(ctx, container) 42 if err != nil { 43 errs = append(errs, err.Error()) 44 } else { 45 fmt.Fprintf(dockerCli.Out(), "%d\n", status) 46 } 47 } 48 if len(errs) > 0 { 49 return fmt.Errorf("%s", strings.Join(errs, "\n")) 50 } 51 return nil 52 }