pause.go 2.6 KB

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