wait.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. Copyright 2023 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. "os"
  17. "github.com/docker/cli/cli"
  18. "github.com/docker/compose/v2/pkg/api"
  19. "github.com/spf13/cobra"
  20. )
  21. type waitOptions struct {
  22. *ProjectOptions
  23. services []string
  24. downProject bool
  25. }
  26. func waitCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
  27. opts := waitOptions{
  28. ProjectOptions: p,
  29. }
  30. var statusCode int64
  31. var err error
  32. cmd := &cobra.Command{
  33. Use: "wait SERVICE [SERVICE...] [OPTIONS]",
  34. Short: "Block until the first service container stops",
  35. Args: cli.RequiresMinArgs(1),
  36. RunE: Adapt(func(ctx context.Context, services []string) error {
  37. opts.services = services
  38. statusCode, err = runWait(ctx, backend, &opts)
  39. return err
  40. }),
  41. PostRun: func(cmd *cobra.Command, args []string) {
  42. os.Exit(int(statusCode))
  43. },
  44. }
  45. cmd.Flags().BoolVar(&opts.downProject, "down-project", false, "Drops project when the first container stops")
  46. return cmd
  47. }
  48. func runWait(ctx context.Context, backend api.Service, opts *waitOptions) (int64, error) {
  49. _, name, err := opts.projectOrName()
  50. if err != nil {
  51. return 0, err
  52. }
  53. return backend.Wait(ctx, name, api.WaitOptions{
  54. Services: opts.services,
  55. DownProjectOnContainerExit: opts.downProject,
  56. })
  57. }