Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions aci/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ func (cs *aciComposeService) Copy(ctx context.Context, project *types.Project, o
}

func (cs *aciComposeService) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error {
return progress.Run(ctx, func(ctx context.Context) error {
return cs.up(ctx, project)
})
}

func (cs *aciComposeService) up(ctx context.Context, project *types.Project) error {
logrus.Debugf("Up on project with name %q", project.Name)

if err := autocreateFileshares(ctx, project); err != nil {
Expand Down
18 changes: 10 additions & 8 deletions api/compose/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,14 @@ type CreateOptions struct {

// StartOptions group options of the Start API
type StartOptions struct {
// Attach will attach to service containers and send container logs and events
Attach ContainerEventListener
// Services passed in the command line to be started
Services []string
// Attach to container and forward logs if not nil
Attach LogConsumer
// AttachTo set the services to attach to
AttachTo []string
// CascadeStop stops the application when a container stops
CascadeStop bool
// ExitCodeFrom return exit code from specified service
ExitCodeFrom string
}

// RestartOptions group options of the Restart API
Expand All @@ -136,10 +140,8 @@ type StopOptions struct {

// UpOptions group options of the Up API
type UpOptions struct {
// Detach will create services and return immediately
Detach bool
// QuietPull makes the pulling process quiet
QuietPull bool
Create CreateOptions
Start StartOptions
}

// DownOptions group options of the Down API
Expand Down
40 changes: 35 additions & 5 deletions cli/cmd/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"

Expand Down Expand Up @@ -90,14 +91,43 @@ type projectOptions struct {
// ProjectFunc does stuff within a types.Project
type ProjectFunc func(ctx context.Context, project *types.Project) error

// ProjectServicesFunc does stuff within a types.Project and a selection of services
type ProjectServicesFunc func(ctx context.Context, project *types.Project, services []string) error

// WithServices creates a cobra run command from a ProjectFunc based on configured project options and selected services
func (o *projectOptions) WithProject(fn ProjectFunc) func(cmd *cobra.Command, args []string) error {
return o.WithServices(func(ctx context.Context, project *types.Project, services []string) error {
return fn(ctx, project)
})
}

// WithServices creates a cobra run command from a ProjectFunc based on configured project options and selected services
func (o *projectOptions) WithServices(services []string, fn ProjectFunc) func(cmd *cobra.Command, args []string) error {
return Adapt(func(ctx context.Context, strings []string) error {
project, err := o.toProject(services)
func (o *projectOptions) WithServices(fn ProjectServicesFunc) func(cmd *cobra.Command, args []string) error {
return Adapt(func(ctx context.Context, args []string) error {
project, err := o.toProject(args)
if err != nil {
return err
}
return fn(ctx, project)

if o.EnvFile != "" {
var services types.Services
for _, s := range project.Services {
ef := o.EnvFile
if ef != "" {
if !filepath.IsAbs(ef) {
ef = filepath.Join(project.WorkingDir, o.EnvFile)
}
if s.Labels == nil {
s.Labels = make(map[string]string)
}
s.Labels[compose.EnvironmentFileLabel] = ef
services = append(services, s)
}
}
project.Services = services
}

return fn(ctx, project, args)
})
}

Expand Down Expand Up @@ -217,7 +247,7 @@ func RootCommand(contextType string, backend compose.Service) *cobra.Command {
}

command.AddCommand(
upCommand(&opts, contextType, backend),
upCommand(&opts, backend),
downCommand(&opts, backend),
startCommand(&opts, backend),
restartCommand(&opts, backend),
Expand Down
78 changes: 63 additions & 15 deletions cli/cmd/compose/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,29 @@ package compose
import (
"context"
"fmt"
"time"

"github.com/compose-spec/compose-go/types"
"github.com/spf13/cobra"

"github.com/docker/compose-cli/api/compose"
)

type createOptions struct {
*composeOptions
Build bool
noBuild bool
removeOrphans bool
forceRecreate bool
noRecreate bool
recreateDeps bool
noInherit bool
timeChanged bool
timeout int
quietPull bool
}

func createCommand(p *projectOptions, backend compose.Service) *cobra.Command {
opts := createOptions{
composeOptions: &composeOptions{},
}
opts := createOptions{}
cmd := &cobra.Command{
Use: "create [SERVICE...]",
Short: "Creates containers for a service.",
Expand All @@ -47,17 +54,15 @@ func createCommand(p *projectOptions, backend compose.Service) *cobra.Command {
}
return nil
}),
RunE: Adapt(func(ctx context.Context, args []string) error {
return runCreateStart(ctx, backend, upOptions{
composeOptions: &composeOptions{
projectOptions: p,
Build: opts.Build,
noBuild: opts.noBuild,
},
noStart: true,
forceRecreate: opts.forceRecreate,
noRecreate: opts.noRecreate,
}, args)
RunE: p.WithProject(func(ctx context.Context, project *types.Project) error {
return backend.Create(ctx, project, compose.CreateOptions{
RemoveOrphans: opts.removeOrphans,
Recreate: opts.recreateStrategy(),
RecreateDependencies: opts.dependenciesRecreateStrategy(),
Inherit: !opts.noInherit,
Timeout: opts.GetTimeout(),
QuietPull: false,
})
}),
}
flags := cmd.Flags()
Expand All @@ -67,3 +72,46 @@ func createCommand(p *projectOptions, backend compose.Service) *cobra.Command {
flags.BoolVar(&opts.noRecreate, "no-recreate", false, "If containers already exist, don't recreate them. Incompatible with --force-recreate.")
return cmd
}

func (opts createOptions) recreateStrategy() string {
if opts.noRecreate {
return compose.RecreateNever
}
if opts.forceRecreate {
return compose.RecreateForce
}
return compose.RecreateDiverged
}

func (opts createOptions) dependenciesRecreateStrategy() string {
if opts.noRecreate {
return compose.RecreateNever
}
if opts.recreateDeps {
return compose.RecreateForce
}
return compose.RecreateDiverged
}

func (opts createOptions) GetTimeout() *time.Duration {
if opts.timeChanged {
t := time.Duration(opts.timeout) * time.Second
return &t
}
return nil
}

func (opts createOptions) Apply(project *types.Project) {
if opts.Build {
for i, service := range project.Services {
service.PullPolicy = types.PullPolicyBuild
project.Services[i] = service
}
}
if opts.noBuild {
for i, service := range project.Services {
service.Build = nil
project.Services[i] = service
}
}
}
3 changes: 1 addition & 2 deletions cli/cmd/compose/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package compose

import (
"context"
"os"

"github.com/compose-spec/compose-go/types"
"github.com/spf13/cobra"
Expand All @@ -31,7 +30,7 @@ func killCommand(p *projectOptions, backend compose.Service) *cobra.Command {
cmd := &cobra.Command{
Use: "kill [options] [SERVICE...]",
Short: "Force stop service containers.",
RunE: p.WithServices(os.Args, func(ctx context.Context, project *types.Project) error {
RunE: p.WithProject(func(ctx context.Context, project *types.Project) error {
return backend.Kill(ctx, project, opts)
}),
}
Expand Down
15 changes: 7 additions & 8 deletions cli/cmd/compose/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ func runCommand(p *projectOptions, backend compose.Service) *cobra.Command {
return nil
}),
RunE: Adapt(func(ctx context.Context, args []string) error {
return runRun(ctx, backend, opts)
project, err := p.toProject([]string{opts.Service})
if err != nil {
return err
}
return runRun(ctx, backend, project, opts)
}),
}
flags := cmd.Flags()
Expand All @@ -143,13 +147,8 @@ func runCommand(p *projectOptions, backend compose.Service) *cobra.Command {
return cmd
}

func runRun(ctx context.Context, backend compose.Service, opts runOptions) error {
project, err := setup(*opts.composeOptions, []string{opts.Service})
if err != nil {
return err
}

err = opts.apply(project)
func runRun(ctx context.Context, backend compose.Service, project *types.Project, opts runOptions) error {
err := opts.apply(project)
if err != nil {
return err
}
Expand Down
Loading