github.1git.de/docker/cli@v26.1.3+incompatible/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/docker/cli/cli/command/completion"
    11  	"github.com/docker/docker/api/types"
    12  	"github.com/pkg/errors"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  type pauseOptions struct {
    17  	containers []string
    18  }
    19  
    20  // NewPauseCommand creates a new cobra.Command for `docker pause`
    21  func NewPauseCommand(dockerCli command.Cli) *cobra.Command {
    22  	var opts pauseOptions
    23  
    24  	return &cobra.Command{
    25  		Use:   "pause CONTAINER [CONTAINER...]",
    26  		Short: "Pause all processes within one or more containers",
    27  		Args:  cli.RequiresMinArgs(1),
    28  		RunE: func(cmd *cobra.Command, args []string) error {
    29  			opts.containers = args
    30  			return runPause(cmd.Context(), dockerCli, &opts)
    31  		},
    32  		Annotations: map[string]string{
    33  			"aliases": "docker container pause, docker pause",
    34  		},
    35  		ValidArgsFunction: completion.ContainerNames(dockerCli, false, func(container types.Container) bool {
    36  			return container.State != "paused"
    37  		}),
    38  	}
    39  }
    40  
    41  func runPause(ctx context.Context, dockerCli command.Cli, opts *pauseOptions) error {
    42  	var errs []string
    43  	errChan := parallelOperation(ctx, opts.containers, dockerCli.Client().ContainerPause)
    44  	for _, container := range opts.containers {
    45  		if err := <-errChan; err != nil {
    46  			errs = append(errs, err.Error())
    47  			continue
    48  		}
    49  		fmt.Fprintln(dockerCli.Out(), container)
    50  	}
    51  	if len(errs) > 0 {
    52  		return errors.New(strings.Join(errs, "\n"))
    53  	}
    54  	return nil
    55  }