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