|
| 1 | +/* |
| 2 | + Copyright 2020 Docker Compose CLI authors |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package compose |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "os" |
| 22 | + |
| 23 | + "github.com/spf13/cobra" |
| 24 | + |
| 25 | + "github.com/docker/compose-cli/api/client" |
| 26 | + "github.com/docker/compose-cli/api/compose" |
| 27 | + "github.com/docker/compose-cli/api/progress" |
| 28 | + "github.com/docker/compose-cli/cli/formatter" |
| 29 | +) |
| 30 | + |
| 31 | +type startOptions struct { |
| 32 | + *projectOptions |
| 33 | + Detach bool |
| 34 | +} |
| 35 | + |
| 36 | +func startCommand(p *projectOptions) *cobra.Command { |
| 37 | + opts := startOptions{ |
| 38 | + projectOptions: p, |
| 39 | + } |
| 40 | + startCmd := &cobra.Command{ |
| 41 | + Use: "start [SERVICE...]", |
| 42 | + Short: "Start services", |
| 43 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 44 | + return runStart(cmd.Context(), opts, args) |
| 45 | + }, |
| 46 | + } |
| 47 | + |
| 48 | + startCmd.Flags().BoolVarP(&opts.Detach, "detach", "d", false, "Detached mode: Run containers in the background") |
| 49 | + return startCmd |
| 50 | +} |
| 51 | + |
| 52 | +func runStart(ctx context.Context, opts startOptions, services []string) error { |
| 53 | + c, err := client.NewWithDefaultLocalBackend(ctx) |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + |
| 58 | + var consumer compose.LogConsumer |
| 59 | + if !opts.Detach { |
| 60 | + consumer = formatter.NewLogConsumer(ctx, os.Stdout) |
| 61 | + } |
| 62 | + _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { |
| 63 | + project, err := opts.toProject() |
| 64 | + if err != nil { |
| 65 | + return "", err |
| 66 | + } |
| 67 | + |
| 68 | + err = filter(project, services) |
| 69 | + if err != nil { |
| 70 | + return "", err |
| 71 | + } |
| 72 | + return "", c.ComposeService().Start(ctx, project, consumer) |
| 73 | + }) |
| 74 | + return err |
| 75 | +} |
0 commit comments