pause.go 2.2 KB

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