kill.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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-cli/api/compose"
  18. )
  19. type killOptions struct {
  20. *projectOptions
  21. Signal string
  22. }
  23. func killCommand(p *projectOptions, backend compose.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: func(cmd *cobra.Command, args []string) error {
  31. return runKill(cmd.Context(), backend, opts, args)
  32. },
  33. }
  34. flags := cmd.Flags()
  35. flags.StringVarP(&opts.Signal, "signal", "s", "SIGKILL", "SIGNAL to send to the container.")
  36. return cmd
  37. }
  38. func runKill(ctx context.Context, backend compose.Service, opts killOptions, services []string) error {
  39. project, err := opts.toProject(services)
  40. if err != nil {
  41. return err
  42. }
  43. return backend.Kill(ctx, project, compose.KillOptions{
  44. Signal: opts.Signal,
  45. })
  46. }