Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit c135bd1

Browse files
committed
move up logic from CLI into local backend
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent b86ff6b commit c135bd1

File tree

15 files changed

+323
-304
lines changed

15 files changed

+323
-304
lines changed

aci/compose.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ func (cs *aciComposeService) Copy(ctx context.Context, project *types.Project, o
8787
}
8888

8989
func (cs *aciComposeService) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error {
90+
return progress.Run(ctx, func(ctx context.Context) error {
91+
return cs.up(ctx, project)
92+
})
93+
}
94+
95+
func (cs *aciComposeService) up(ctx context.Context, project *types.Project) error {
9096
logrus.Debugf("Up on project with name %q", project.Name)
9197

9298
if err := autocreateFileshares(ctx, project); err != nil {

api/compose/api.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,14 @@ type CreateOptions struct {
112112

113113
// StartOptions group options of the Start API
114114
type StartOptions struct {
115-
// Attach will attach to service containers and send container logs and events
116-
Attach ContainerEventListener
117-
// Services passed in the command line to be started
118-
Services []string
115+
// Attach to container and forward logs if not nil
116+
Attach LogConsumer
117+
// AttachTo set the services to attach to
118+
AttachTo []string
119+
// CascadeStop stops the application when a container stops
120+
CascadeStop bool
121+
// ExitCodeFrom return exit code from specified service
122+
ExitCodeFrom string
119123
}
120124

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

137141
// UpOptions group options of the Up API
138142
type UpOptions struct {
139-
// Detach will create services and return immediately
140-
Detach bool
141-
// QuietPull makes the pulling process quiet
142-
QuietPull bool
143+
Create CreateOptions
144+
Start StartOptions
143145
}
144146

145147
// DownOptions group options of the Down API

cli/cmd/compose/compose.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"os"
2323
"os/signal"
24+
"path/filepath"
2425
"strings"
2526
"syscall"
2627

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

94+
// ProjectServicesFunc does stuff within a types.Project and a selection of services
95+
type ProjectServicesFunc func(ctx context.Context, project *types.Project, services []string) error
96+
97+
// WithServices creates a cobra run command from a ProjectFunc based on configured project options and selected services
98+
func (o *projectOptions) WithProject(fn ProjectFunc) func(cmd *cobra.Command, args []string) error {
99+
return o.WithServices(func(ctx context.Context, project *types.Project, services []string) error {
100+
return fn(ctx, project)
101+
})
102+
}
103+
93104
// WithServices creates a cobra run command from a ProjectFunc based on configured project options and selected services
94-
func (o *projectOptions) WithServices(services []string, fn ProjectFunc) func(cmd *cobra.Command, args []string) error {
95-
return Adapt(func(ctx context.Context, strings []string) error {
96-
project, err := o.toProject(services)
105+
func (o *projectOptions) WithServices(fn ProjectServicesFunc) func(cmd *cobra.Command, args []string) error {
106+
return Adapt(func(ctx context.Context, args []string) error {
107+
project, err := o.toProject(args)
97108
if err != nil {
98109
return err
99110
}
100-
return fn(ctx, project)
111+
112+
if o.EnvFile != "" {
113+
var services types.Services
114+
for _, s := range project.Services {
115+
ef := o.EnvFile
116+
if ef != "" {
117+
if !filepath.IsAbs(ef) {
118+
ef = filepath.Join(project.WorkingDir, o.EnvFile)
119+
}
120+
if s.Labels == nil {
121+
s.Labels = make(map[string]string)
122+
}
123+
s.Labels[compose.EnvironmentFileLabel] = ef
124+
services = append(services, s)
125+
}
126+
}
127+
project.Services = services
128+
}
129+
130+
return fn(ctx, project, args)
101131
})
102132
}
103133

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

219249
command.AddCommand(
220-
upCommand(&opts, contextType, backend),
250+
upCommand(&opts, backend),
221251
downCommand(&opts, backend),
222252
startCommand(&opts, backend),
223253
restartCommand(&opts, backend),

cli/cmd/compose/create.go

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,29 @@ package compose
1919
import (
2020
"context"
2121
"fmt"
22+
"time"
2223

24+
"github.com/compose-spec/compose-go/types"
2325
"github.com/spf13/cobra"
2426

2527
"github.com/docker/compose-cli/api/compose"
2628
)
2729

2830
type createOptions struct {
29-
*composeOptions
31+
Build bool
32+
noBuild bool
33+
removeOrphans bool
3034
forceRecreate bool
3135
noRecreate bool
36+
recreateDeps bool
37+
noInherit bool
38+
timeChanged bool
39+
timeout int
40+
quietPull bool
3241
}
3342

3443
func createCommand(p *projectOptions, backend compose.Service) *cobra.Command {
35-
opts := createOptions{
36-
composeOptions: &composeOptions{},
37-
}
44+
opts := createOptions{}
3845
cmd := &cobra.Command{
3946
Use: "create [SERVICE...]",
4047
Short: "Creates containers for a service.",
@@ -47,17 +54,15 @@ func createCommand(p *projectOptions, backend compose.Service) *cobra.Command {
4754
}
4855
return nil
4956
}),
50-
RunE: Adapt(func(ctx context.Context, args []string) error {
51-
return runCreateStart(ctx, backend, upOptions{
52-
composeOptions: &composeOptions{
53-
projectOptions: p,
54-
Build: opts.Build,
55-
noBuild: opts.noBuild,
56-
},
57-
noStart: true,
58-
forceRecreate: opts.forceRecreate,
59-
noRecreate: opts.noRecreate,
60-
}, args)
57+
RunE: p.WithProject(func(ctx context.Context, project *types.Project) error {
58+
return backend.Create(ctx, project, compose.CreateOptions{
59+
RemoveOrphans: opts.removeOrphans,
60+
Recreate: opts.recreateStrategy(),
61+
RecreateDependencies: opts.dependenciesRecreateStrategy(),
62+
Inherit: !opts.noInherit,
63+
Timeout: opts.GetTimeout(),
64+
QuietPull: false,
65+
})
6166
}),
6267
}
6368
flags := cmd.Flags()
@@ -67,3 +72,46 @@ func createCommand(p *projectOptions, backend compose.Service) *cobra.Command {
6772
flags.BoolVar(&opts.noRecreate, "no-recreate", false, "If containers already exist, don't recreate them. Incompatible with --force-recreate.")
6873
return cmd
6974
}
75+
76+
func (opts createOptions) recreateStrategy() string {
77+
if opts.noRecreate {
78+
return compose.RecreateNever
79+
}
80+
if opts.forceRecreate {
81+
return compose.RecreateForce
82+
}
83+
return compose.RecreateDiverged
84+
}
85+
86+
func (opts createOptions) dependenciesRecreateStrategy() string {
87+
if opts.noRecreate {
88+
return compose.RecreateNever
89+
}
90+
if opts.recreateDeps {
91+
return compose.RecreateForce
92+
}
93+
return compose.RecreateDiverged
94+
}
95+
96+
func (opts createOptions) GetTimeout() *time.Duration {
97+
if opts.timeChanged {
98+
t := time.Duration(opts.timeout) * time.Second
99+
return &t
100+
}
101+
return nil
102+
}
103+
104+
func (opts createOptions) Apply(project *types.Project) {
105+
if opts.Build {
106+
for i, service := range project.Services {
107+
service.PullPolicy = types.PullPolicyBuild
108+
project.Services[i] = service
109+
}
110+
}
111+
if opts.noBuild {
112+
for i, service := range project.Services {
113+
service.Build = nil
114+
project.Services[i] = service
115+
}
116+
}
117+
}

cli/cmd/compose/kill.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package compose
1818

1919
import (
2020
"context"
21-
"os"
2221

2322
"github.com/compose-spec/compose-go/types"
2423
"github.com/spf13/cobra"
@@ -31,7 +30,7 @@ func killCommand(p *projectOptions, backend compose.Service) *cobra.Command {
3130
cmd := &cobra.Command{
3231
Use: "kill [options] [SERVICE...]",
3332
Short: "Force stop service containers.",
34-
RunE: p.WithServices(os.Args, func(ctx context.Context, project *types.Project) error {
33+
RunE: p.WithProject(func(ctx context.Context, project *types.Project) error {
3534
return backend.Kill(ctx, project, opts)
3635
}),
3736
}

cli/cmd/compose/run.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,11 @@ func runCommand(p *projectOptions, backend compose.Service) *cobra.Command {
120120
return nil
121121
}),
122122
RunE: Adapt(func(ctx context.Context, args []string) error {
123-
return runRun(ctx, backend, opts)
123+
project, err := p.toProject([]string{opts.Service})
124+
if err != nil {
125+
return err
126+
}
127+
return runRun(ctx, backend, project, opts)
124128
}),
125129
}
126130
flags := cmd.Flags()
@@ -143,13 +147,8 @@ func runCommand(p *projectOptions, backend compose.Service) *cobra.Command {
143147
return cmd
144148
}
145149

146-
func runRun(ctx context.Context, backend compose.Service, opts runOptions) error {
147-
project, err := setup(*opts.composeOptions, []string{opts.Service})
148-
if err != nil {
149-
return err
150-
}
151-
152-
err = opts.apply(project)
150+
func runRun(ctx context.Context, backend compose.Service, project *types.Project, opts runOptions) error {
151+
err := opts.apply(project)
153152
if err != nil {
154153
return err
155154
}

0 commit comments

Comments
 (0)