kill.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "os"
  17. "github.com/spf13/cobra"
  18. "github.com/docker/compose/v2/pkg/api"
  19. "github.com/docker/compose/v2/pkg/utils"
  20. )
  21. type killOptions struct {
  22. *ProjectOptions
  23. removeOrphans bool
  24. signal string
  25. }
  26. func killCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
  27. opts := killOptions{
  28. ProjectOptions: p,
  29. }
  30. cmd := &cobra.Command{
  31. Use: "kill [OPTIONS] [SERVICE...]",
  32. Short: "Force stop service containers.",
  33. RunE: Adapt(func(ctx context.Context, args []string) error {
  34. return runKill(ctx, backend, opts, args)
  35. }),
  36. ValidArgsFunction: completeServiceNames(p),
  37. }
  38. flags := cmd.Flags()
  39. removeOrphans := utils.StringToBool(os.Getenv("COMPOSE_REMOVE_ORPHANS"))
  40. flags.BoolVar(&opts.removeOrphans, "remove-orphans", removeOrphans, "Remove containers for services not defined in the Compose file.")
  41. flags.StringVarP(&opts.signal, "signal", "s", "SIGKILL", "SIGNAL to send to the container.")
  42. return cmd
  43. }
  44. func runKill(ctx context.Context, backend api.Service, opts killOptions, services []string) error {
  45. project, name, err := opts.projectOrName(services...)
  46. if err != nil {
  47. return err
  48. }
  49. return backend.Kill(ctx, name, api.KillOptions{
  50. RemoveOrphans: opts.removeOrphans,
  51. Project: project,
  52. Services: services,
  53. Signal: opts.signal,
  54. })
  55. }