start.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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/docker/compose/v2/pkg/api"
  18. "github.com/spf13/cobra"
  19. )
  20. type startOptions struct {
  21. *ProjectOptions
  22. }
  23. func startCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command {
  24. opts := startOptions{
  25. ProjectOptions: p,
  26. }
  27. startCmd := &cobra.Command{
  28. Use: "start [SERVICE...]",
  29. Short: "Start services",
  30. RunE: Adapt(func(ctx context.Context, args []string) error {
  31. return runStart(ctx, dockerCli, backend, opts, args)
  32. }),
  33. ValidArgsFunction: completeServiceNames(dockerCli, p),
  34. }
  35. return startCmd
  36. }
  37. func runStart(ctx context.Context, dockerCli command.Cli, backend api.Service, opts startOptions, services []string) error {
  38. project, name, err := opts.projectOrName(ctx, dockerCli, services...)
  39. if err != nil {
  40. return err
  41. }
  42. return backend.Start(ctx, name, api.StartOptions{
  43. AttachTo: services,
  44. Project: project,
  45. Services: services,
  46. })
  47. }