github.com/AliyunContainerService/cli@v0.0.0-20181009023821-814ced4b30d0/cli/command/container/pause.go (about)

     1  package container
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/docker/cli/cli"
     9  	"github.com/docker/cli/cli/command"
    10  	"github.com/pkg/errors"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  type pauseOptions struct {
    15  	containers []string
    16  }
    17  
    18  // NewPauseCommand creates a new cobra.Command for `docker pause`
    19  func NewPauseCommand(dockerCli command.Cli) *cobra.Command {
    20  	var opts pauseOptions
    21  
    22  	return &cobra.Command{
    23  		Use:   "pause CONTAINER [CONTAINER...]",
    24  		Short: "Pause all processes within one or more containers",
    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  
    33  func runPause(dockerCli command.Cli, opts *pauseOptions) error {
    34  	ctx := context.Background()
    35  
    36  	var errs []string
    37  	errChan := parallelOperation(ctx, opts.containers, dockerCli.Client().ContainerPause)
    38  	for _, container := range opts.containers {
    39  		if err := <-errChan; err != nil {
    40  			errs = append(errs, err.Error())
    41  			continue
    42  		}
    43  		fmt.Fprintln(dockerCli.Out(), container)
    44  	}
    45  	if len(errs) > 0 {
    46  		return errors.New(strings.Join(errs, "\n"))
    47  	}
    48  	return nil
    49  }