github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/compose_kill.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"github.com/containerd/nerdctl/pkg/clientutil"
    21  	"github.com/containerd/nerdctl/pkg/cmd/compose"
    22  	"github.com/containerd/nerdctl/pkg/composer"
    23  	"github.com/spf13/cobra"
    24  )
    25  
    26  func newComposeKillCommand() *cobra.Command {
    27  	var composeKillCommand = &cobra.Command{
    28  		Use:           "kill [flags] [SERVICE...]",
    29  		Short:         "Force stop service containers",
    30  		RunE:          composeKillAction,
    31  		SilenceUsage:  true,
    32  		SilenceErrors: true,
    33  	}
    34  	composeKillCommand.Flags().StringP("signal", "s", "SIGKILL", "SIGNAL to send to the container.")
    35  	return composeKillCommand
    36  }
    37  
    38  func composeKillAction(cmd *cobra.Command, args []string) error {
    39  	globalOptions, err := processRootCmdFlags(cmd)
    40  	if err != nil {
    41  		return err
    42  	}
    43  	signal, err := cmd.Flags().GetString("signal")
    44  	if err != nil {
    45  		return err
    46  	}
    47  
    48  	client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address)
    49  	if err != nil {
    50  		return err
    51  	}
    52  	defer cancel()
    53  	options, err := getComposeOptions(cmd, globalOptions.DebugFull, globalOptions.Experimental)
    54  	if err != nil {
    55  		return err
    56  	}
    57  	c, err := compose.New(client, globalOptions, options, cmd.OutOrStdout(), cmd.ErrOrStderr())
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	killOpts := composer.KillOptions{
    63  		Signal: signal,
    64  	}
    65  	return c.Kill(ctx, killOpts, args)
    66  }