1
0

stop.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. "time"
  17. "github.com/spf13/cobra"
  18. "github.com/docker/compose/v2/pkg/api"
  19. )
  20. type stopOptions struct {
  21. *projectOptions
  22. timeChanged bool
  23. timeout int
  24. }
  25. func stopCommand(p *projectOptions, backend api.Service) *cobra.Command {
  26. opts := stopOptions{
  27. projectOptions: p,
  28. }
  29. cmd := &cobra.Command{
  30. Use: "stop [SERVICE...]",
  31. Short: "Stop services",
  32. PreRun: func(cmd *cobra.Command, args []string) {
  33. opts.timeChanged = cmd.Flags().Changed("timeout")
  34. },
  35. RunE: Adapt(func(ctx context.Context, args []string) error {
  36. return runStop(ctx, backend, opts, args)
  37. }),
  38. ValidArgsFunction: serviceCompletion(p),
  39. }
  40. flags := cmd.Flags()
  41. flags.IntVarP(&opts.timeout, "timeout", "t", 10, "Specify a shutdown timeout in seconds")
  42. return cmd
  43. }
  44. func runStop(ctx context.Context, backend api.Service, opts stopOptions, services []string) error {
  45. project, err := opts.toProject(services)
  46. if err != nil {
  47. return err
  48. }
  49. var timeout *time.Duration
  50. if opts.timeChanged {
  51. timeoutValue := time.Duration(opts.timeout) * time.Second
  52. timeout = &timeoutValue
  53. }
  54. return backend.Stop(ctx, project, api.StopOptions{
  55. Timeout: timeout,
  56. Services: services,
  57. })
  58. }