pause.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 pauseOptions struct {
  12. containers []string
  13. }
  14. // NewPauseCommand creates a new cobra.Command for `docker pause`
  15. func NewPauseCommand(dockerCli *command.DockerCli) *cobra.Command {
  16. var opts pauseOptions
  17. return &cobra.Command{
  18. Use: "pause CONTAINER [CONTAINER...]",
  19. Short: "Pause all processes within one or more containers",
  20. Args: cli.RequiresMinArgs(1),
  21. RunE: func(cmd *cobra.Command, args []string) error {
  22. opts.containers = args
  23. return runPause(dockerCli, &opts)
  24. },
  25. }
  26. }
  27. func runPause(dockerCli *command.DockerCli, opts *pauseOptions) error {
  28. ctx := context.Background()
  29. var errs []string
  30. errChan := parallelOperation(ctx, opts.containers, dockerCli.Client().ContainerPause)
  31. for _, container := range opts.containers {
  32. if err := <-errChan; err != nil {
  33. errs = append(errs, err.Error())
  34. continue
  35. }
  36. fmt.Fprintln(dockerCli.Out(), container)
  37. }
  38. if len(errs) > 0 {
  39. return errors.New(strings.Join(errs, "\n"))
  40. }
  41. return nil
  42. }