start.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. "os"
  17. "github.com/spf13/cobra"
  18. "github.com/docker/compose-cli/api/client"
  19. "github.com/docker/compose-cli/api/progress"
  20. "github.com/docker/compose-cli/cli/formatter"
  21. )
  22. type startOptions struct {
  23. *projectOptions
  24. Detach bool
  25. }
  26. func startCommand(p *projectOptions) *cobra.Command {
  27. opts := startOptions{
  28. projectOptions: p,
  29. }
  30. startCmd := &cobra.Command{
  31. Use: "start [SERVICE...]",
  32. Short: "Start services",
  33. RunE: func(cmd *cobra.Command, args []string) error {
  34. return runStart(cmd.Context(), opts, args)
  35. },
  36. }
  37. startCmd.Flags().BoolVarP(&opts.Detach, "detach", "d", false, "Detached mode: Run containers in the background")
  38. return startCmd
  39. }
  40. func runStart(ctx context.Context, opts startOptions, services []string) error {
  41. c, err := client.NewWithDefaultLocalBackend(ctx)
  42. if err != nil {
  43. return err
  44. }
  45. project, err := opts.toProject()
  46. if err != nil {
  47. return err
  48. }
  49. err = filter(project, services)
  50. if err != nil {
  51. return err
  52. }
  53. if opts.Detach {
  54. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  55. return "", c.ComposeService().Start(ctx, project, nil)
  56. })
  57. return err
  58. }
  59. return c.ComposeService().Start(ctx, project, formatter.NewLogConsumer(ctx, os.Stdout))
  60. }