kill.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. "github.com/spf13/cobra"
  17. "github.com/docker/compose/v2/pkg/api"
  18. )
  19. type killOptions struct {
  20. *projectOptions
  21. signal string
  22. }
  23. func killCommand(p *projectOptions, backend api.Service) *cobra.Command {
  24. opts := killOptions{
  25. projectOptions: p,
  26. }
  27. cmd := &cobra.Command{
  28. Use: "kill [options] [SERVICE...]",
  29. Short: "Force stop service containers.",
  30. RunE: Adapt(func(ctx context.Context, args []string) error {
  31. return runKill(ctx, backend, opts, args)
  32. }),
  33. ValidArgsFunction: serviceCompletion(p),
  34. }
  35. flags := cmd.Flags()
  36. flags.StringVarP(&opts.signal, "signal", "s", "SIGKILL", "SIGNAL to send to the container.")
  37. return cmd
  38. }
  39. func runKill(ctx context.Context, backend api.Service, opts killOptions, services []string) error {
  40. projectName, err := opts.toProjectName()
  41. if err != nil {
  42. return err
  43. }
  44. return backend.Kill(ctx, projectName, api.KillOptions{
  45. Services: services,
  46. Signal: opts.signal,
  47. })
  48. }