github.com/DaoCloud/dao@v0.0.0-20161212064103-c3dbfd13ee36/api/client/container/pause.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 pauseOptions struct { 15 containers []string 16 } 17 18 // NewPauseCommand creats a new cobra.Command for `docker pause` 19 func NewPauseCommand(dockerCli *client.DockerCli) *cobra.Command { 20 var opts pauseOptions 21 22 cmd := &cobra.Command{ 23 Use: "pause CONTAINER [CONTAINER...]", 24 Short: "暂停一个或多个容器内部的所有进程的运行", 25 Args: cli.RequiresMinArgs(1), 26 RunE: func(cmd *cobra.Command, args []string) error { 27 opts.containers = args 28 return runPause(dockerCli, &opts) 29 }, 30 } 31 32 return cmd 33 } 34 35 func runPause(dockerCli *client.DockerCli, opts *pauseOptions) error { 36 ctx := context.Background() 37 38 var errs []string 39 for _, container := range opts.containers { 40 if err := dockerCli.Client().ContainerPause(ctx, container); err != nil { 41 errs = append(errs, err.Error()) 42 } else { 43 fmt.Fprintf(dockerCli.Out(), "%s\n", container) 44 } 45 } 46 if len(errs) > 0 { 47 return fmt.Errorf("%s", strings.Join(errs, "\n")) 48 } 49 return nil 50 }