pause.go 1.1 KB

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