github.com/kunnos/engine@v1.13.1/cli/command/container/kill.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 killOptions struct {
    15  	signal string
    16  
    17  	containers []string
    18  }
    19  
    20  // NewKillCommand creates a new cobra.Command for `docker kill`
    21  func NewKillCommand(dockerCli *command.DockerCli) *cobra.Command {
    22  	var opts killOptions
    23  
    24  	cmd := &cobra.Command{
    25  		Use:   "kill [OPTIONS] CONTAINER [CONTAINER...]",
    26  		Short: "Kill one or more running containers",
    27  		Args:  cli.RequiresMinArgs(1),
    28  		RunE: func(cmd *cobra.Command, args []string) error {
    29  			opts.containers = args
    30  			return runKill(dockerCli, &opts)
    31  		},
    32  	}
    33  
    34  	flags := cmd.Flags()
    35  	flags.StringVarP(&opts.signal, "signal", "s", "KILL", "Signal to send to the container")
    36  	return cmd
    37  }
    38  
    39  func runKill(dockerCli *command.DockerCli, opts *killOptions) error {
    40  	var errs []string
    41  	ctx := context.Background()
    42  	errChan := parallelOperation(ctx, opts.containers, func(ctx context.Context, container string) error {
    43  		return dockerCli.Client().ContainerKill(ctx, container, opts.signal)
    44  	})
    45  	for _, name := range opts.containers {
    46  		if err := <-errChan; err != nil {
    47  			errs = append(errs, err.Error())
    48  		} else {
    49  			fmt.Fprintf(dockerCli.Out(), "%s\n", name)
    50  		}
    51  	}
    52  	if len(errs) > 0 {
    53  		return fmt.Errorf("%s", strings.Join(errs, "\n"))
    54  	}
    55  	return nil
    56  }