pause.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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/docker/cli/cli/command"
  17. "github.com/spf13/cobra"
  18. "github.com/docker/compose/v2/pkg/api"
  19. )
  20. type pauseOptions struct {
  21. *ProjectOptions
  22. }
  23. func pauseCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command {
  24. opts := pauseOptions{
  25. ProjectOptions: p,
  26. }
  27. cmd := &cobra.Command{
  28. Use: "pause [SERVICE...]",
  29. Short: "Pause services",
  30. RunE: Adapt(func(ctx context.Context, args []string) error {
  31. return runPause(ctx, dockerCli, backend, opts, args)
  32. }),
  33. ValidArgsFunction: completeServiceNames(dockerCli, p),
  34. }
  35. return cmd
  36. }
  37. func runPause(ctx context.Context, dockerCli command.Cli, backend api.Service, opts pauseOptions, services []string) error {
  38. project, name, err := opts.projectOrName(ctx, dockerCli, services...)
  39. if err != nil {
  40. return err
  41. }
  42. return backend.Pause(ctx, name, api.PauseOptions{
  43. Services: services,
  44. Project: project,
  45. })
  46. }
  47. type unpauseOptions struct {
  48. *ProjectOptions
  49. }
  50. func unpauseCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command {
  51. opts := unpauseOptions{
  52. ProjectOptions: p,
  53. }
  54. cmd := &cobra.Command{
  55. Use: "unpause [SERVICE...]",
  56. Short: "Unpause services",
  57. RunE: Adapt(func(ctx context.Context, args []string) error {
  58. return runUnPause(ctx, dockerCli, backend, opts, args)
  59. }),
  60. ValidArgsFunction: completeServiceNames(dockerCli, p),
  61. }
  62. return cmd
  63. }
  64. func runUnPause(ctx context.Context, dockerCli command.Cli, backend api.Service, opts unpauseOptions, services []string) error {
  65. project, name, err := opts.projectOrName(ctx, dockerCli, services...)
  66. if err != nil {
  67. return err
  68. }
  69. return backend.UnPause(ctx, name, api.PauseOptions{
  70. Services: services,
  71. Project: project,
  72. })
  73. }