mirror of
https://github.com/docker/compose.git
synced 2026-08-27 03:45:29 +00:00
split Up into Create+Start so logs don't collide with progress
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
parent
39e4107e12
commit
136d09e1ac
14 changed files with 198 additions and 68 deletions
|
|
@ -18,14 +18,18 @@ package compose
|
|||
|
||||
import (
|
||||
"context"
|
||||
"github.com/docker/compose-cli/progress"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/compose-spec/compose-go/cli"
|
||||
"github.com/compose-spec/compose-go/types"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/docker/compose-cli/api/client"
|
||||
"github.com/docker/compose-cli/context/store"
|
||||
"github.com/docker/compose-cli/progress"
|
||||
)
|
||||
|
||||
func upCommand(contextType string) *cobra.Command {
|
||||
|
|
@ -33,7 +37,12 @@ func upCommand(contextType string) *cobra.Command {
|
|||
upCmd := &cobra.Command{
|
||||
Use: "up [SERVICE...]",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return runUp(cmd.Context(), opts, args)
|
||||
switch contextType {
|
||||
case store.LocalContextType:
|
||||
return runCreateStart(cmd.Context(), opts, args)
|
||||
default:
|
||||
return runUp(cmd.Context(), opts, args)
|
||||
}
|
||||
},
|
||||
}
|
||||
upCmd.Flags().StringVarP(&opts.Name, "project-name", "p", "", "Project name")
|
||||
|
|
@ -50,19 +59,60 @@ func upCommand(contextType string) *cobra.Command {
|
|||
}
|
||||
|
||||
func runUp(ctx context.Context, opts composeOptions, services []string) error {
|
||||
c, err := client.New(ctx)
|
||||
c, project, err := setup(ctx, opts, services)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
options, err := opts.toProjectOptions()
|
||||
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
|
||||
return "", c.ComposeService().Up(ctx, project, opts.Detach)
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func runCreateStart(ctx context.Context, opts composeOptions, services []string) error {
|
||||
c, project, err := setup(ctx, opts, services)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
project, err := cli.ProjectFromOptions(options)
|
||||
|
||||
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
|
||||
return "", c.ComposeService().Create(ctx, project)
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var w io.Writer
|
||||
if !opts.Detach {
|
||||
w = os.Stdout
|
||||
}
|
||||
|
||||
err = c.ComposeService().Start(ctx, project, w)
|
||||
if errors.Is(ctx.Err(), context.Canceled) {
|
||||
fmt.Println("Gracefully stopping...")
|
||||
ctx = context.Background()
|
||||
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
|
||||
return "", c.ComposeService().Down(ctx, project.Name)
|
||||
})
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func setup(ctx context.Context, opts composeOptions, services []string) (*client.Client, *types.Project, error) {
|
||||
c, err := client.New(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
options, err := opts.toProjectOptions()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
project, err := cli.ProjectFromOptions(options)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if opts.DomainName != "" {
|
||||
// arbitrarily set the domain name on the first service ; ACI backend will expose the entire project
|
||||
project.Services[0].DomainName = opts.DomainName
|
||||
|
|
@ -70,11 +120,7 @@ func runUp(ctx context.Context, opts composeOptions, services []string) error {
|
|||
|
||||
err = filter(project, services)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
|
||||
return "", c.ComposeService().Up(ctx, project, opts.Detach, os.Stdout)
|
||||
})
|
||||
return err
|
||||
return c, project, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue