2021-01-31 14:42:13 -03:00
|
|
|
/*
|
|
|
|
Copyright 2020 Docker Compose CLI authors
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package compose
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-08-19 04:04:46 +02:00
|
|
|
"os"
|
2021-01-31 14:42:13 -03:00
|
|
|
|
2023-08-30 10:15:35 +02:00
|
|
|
"github.com/docker/cli/cli/command"
|
2021-01-31 14:42:13 -03:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2021-08-31 18:53:24 +02:00
|
|
|
"github.com/docker/compose/v2/pkg/api"
|
2022-08-19 04:04:46 +02:00
|
|
|
"github.com/docker/compose/v2/pkg/utils"
|
2021-01-31 14:42:13 -03:00
|
|
|
)
|
|
|
|
|
2022-03-14 08:16:26 +01:00
|
|
|
type killOptions struct {
|
2022-12-19 16:38:36 +00:00
|
|
|
*ProjectOptions
|
2022-08-19 04:04:46 +02:00
|
|
|
removeOrphans bool
|
|
|
|
signal string
|
2022-03-14 08:16:26 +01:00
|
|
|
}
|
|
|
|
|
2023-08-30 10:15:35 +02:00
|
|
|
func killCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command {
|
2022-03-14 08:16:26 +01:00
|
|
|
opts := killOptions{
|
2022-12-19 16:38:36 +00:00
|
|
|
ProjectOptions: p,
|
2022-03-14 08:16:26 +01:00
|
|
|
}
|
2021-01-31 14:42:13 -03:00
|
|
|
cmd := &cobra.Command{
|
2022-08-02 13:11:57 +02:00
|
|
|
Use: "kill [OPTIONS] [SERVICE...]",
|
2024-02-14 20:02:37 +01:00
|
|
|
Short: "Force stop service containers",
|
2022-03-14 08:16:26 +01:00
|
|
|
RunE: Adapt(func(ctx context.Context, args []string) error {
|
2023-08-30 10:15:35 +02:00
|
|
|
return runKill(ctx, dockerCli, backend, opts, args)
|
2021-04-15 12:43:18 +02:00
|
|
|
}),
|
2023-08-30 10:15:35 +02:00
|
|
|
ValidArgsFunction: completeServiceNames(dockerCli, p),
|
2021-01-31 14:42:13 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2023-05-14 16:50:41 +02:00
|
|
|
removeOrphans := utils.StringToBool(os.Getenv(ComposeRemoveOrphans))
|
2024-02-14 20:02:37 +01:00
|
|
|
flags.BoolVar(&opts.removeOrphans, "remove-orphans", removeOrphans, "Remove containers for services not defined in the Compose file")
|
|
|
|
flags.StringVarP(&opts.signal, "signal", "s", "SIGKILL", "SIGNAL to send to the container")
|
2021-01-31 14:42:13 -03:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
2022-03-14 08:16:26 +01:00
|
|
|
|
2023-08-30 10:15:35 +02:00
|
|
|
func runKill(ctx context.Context, dockerCli command.Cli, backend api.Service, opts killOptions, services []string) error {
|
2024-02-05 17:38:15 -05:00
|
|
|
project, name, err := opts.projectOrName(ctx, dockerCli, services...)
|
2022-03-14 08:16:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-11 10:58:20 +02:00
|
|
|
return backend.Kill(ctx, name, api.KillOptions{
|
2022-08-19 04:04:46 +02:00
|
|
|
RemoveOrphans: opts.removeOrphans,
|
|
|
|
Project: project,
|
|
|
|
Services: services,
|
|
|
|
Signal: opts.signal,
|
2022-03-14 08:16:26 +01:00
|
|
|
})
|
|
|
|
}
|