github.com/kunnos/engine@v1.13.1/cli/command/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/cli"
    10  	"github.com/docker/docker/cli/command"
    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.DockerCli) *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.DockerCli, 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  		} else {
    42  			fmt.Fprintf(dockerCli.Out(), "%s\n", container)
    43  		}
    44  	}
    45  	if len(errs) > 0 {
    46  		return fmt.Errorf("%s", strings.Join(errs, "\n"))
    47  	}
    48  	return nil
    49  }