start.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. "time"
  17. "github.com/docker/cli/cli/command"
  18. "github.com/spf13/cobra"
  19. "github.com/docker/compose/v5/pkg/api"
  20. "github.com/docker/compose/v5/pkg/compose"
  21. )
  22. type startOptions struct {
  23. *ProjectOptions
  24. wait bool
  25. waitTimeout int
  26. }
  27. func startCommand(p *ProjectOptions, dockerCli command.Cli, backendOptions *BackendOptions) *cobra.Command {
  28. opts := startOptions{
  29. ProjectOptions: p,
  30. }
  31. startCmd := &cobra.Command{
  32. Use: "start [SERVICE...]",
  33. Short: "Start services",
  34. RunE: Adapt(func(ctx context.Context, args []string) error {
  35. return runStart(ctx, dockerCli, backendOptions, opts, args)
  36. }),
  37. ValidArgsFunction: completeServiceNames(dockerCli, p),
  38. }
  39. flags := startCmd.Flags()
  40. flags.BoolVar(&opts.wait, "wait", false, "Wait for services to be running|healthy. Implies detached mode.")
  41. flags.IntVar(&opts.waitTimeout, "wait-timeout", 0, "Maximum duration in seconds to wait for the project to be running|healthy")
  42. return startCmd
  43. }
  44. func runStart(ctx context.Context, dockerCli command.Cli, backendOptions *BackendOptions, opts startOptions, services []string) error {
  45. project, name, err := opts.projectOrName(ctx, dockerCli, services...)
  46. if err != nil {
  47. return err
  48. }
  49. backend, err := compose.NewComposeService(dockerCli, backendOptions.Options...)
  50. if err != nil {
  51. return err
  52. }
  53. timeout := time.Duration(opts.waitTimeout) * time.Second
  54. return backend.Start(ctx, name, api.StartOptions{
  55. AttachTo: services,
  56. Project: project,
  57. Services: services,
  58. Wait: opts.wait,
  59. WaitTimeout: timeout,
  60. })
  61. }