pause.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/client"
  18. "github.com/docker/compose-cli/api/progress"
  19. )
  20. type pauseOptions struct {
  21. *projectOptions
  22. }
  23. func pauseCommand(p *projectOptions) *cobra.Command {
  24. opts := pauseOptions{
  25. projectOptions: p,
  26. }
  27. cmd := &cobra.Command{
  28. Use: "pause [SERVICE...]",
  29. Short: "pause services",
  30. RunE: func(cmd *cobra.Command, args []string) error {
  31. return runPause(cmd.Context(), opts, args)
  32. },
  33. }
  34. return cmd
  35. }
  36. func runPause(ctx context.Context, opts pauseOptions, services []string) error {
  37. c, err := client.NewWithDefaultLocalBackend(ctx)
  38. if err != nil {
  39. return err
  40. }
  41. project, err := opts.toProject(services)
  42. if err != nil {
  43. return err
  44. }
  45. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  46. return "", c.ComposeService().Pause(ctx, project)
  47. })
  48. return err
  49. }
  50. type unpauseOptions struct {
  51. *projectOptions
  52. }
  53. func unpauseCommand(p *projectOptions) *cobra.Command {
  54. opts := unpauseOptions{
  55. projectOptions: p,
  56. }
  57. cmd := &cobra.Command{
  58. Use: "unpause [SERVICE...]",
  59. Short: "unpause services",
  60. RunE: func(cmd *cobra.Command, args []string) error {
  61. return runUnPause(cmd.Context(), opts, args)
  62. },
  63. }
  64. return cmd
  65. }
  66. func runUnPause(ctx context.Context, opts unpauseOptions, services []string) error {
  67. c, err := client.NewWithDefaultLocalBackend(ctx)
  68. if err != nil {
  69. return err
  70. }
  71. project, err := opts.toProject(services)
  72. if err != nil {
  73. return err
  74. }
  75. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  76. return "", c.ComposeService().UnPause(ctx, project)
  77. })
  78. return err
  79. }