Skip to content

Commit f6e96dd

Browse files
ndelooflaurazard
authored andcommitted
if command is ran with a compose file, apply the compose model, not just project name
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 765c071 commit f6e96dd

20 files changed

+121
-76
lines changed

cmd/compose/compose.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,20 @@ func (o *projectOptions) addProjectFlags(f *pflag.FlagSet) {
136136
_ = f.MarkHidden("workdir")
137137
}
138138

139+
func (o *projectOptions) projectOrName() (*types.Project, string, error) {
140+
name := o.ProjectName
141+
var project *types.Project
142+
if o.ProjectName == "" {
143+
p, err := o.toProject(nil)
144+
if err != nil {
145+
return nil, "", err
146+
}
147+
project = p
148+
name = p.Name
149+
}
150+
return project, name, nil
151+
}
152+
139153
func (o *projectOptions) toProjectName() (string, error) {
140154
if o.ProjectName != "" {
141155
return o.ProjectName, nil

cmd/compose/down.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"os"
2323
"time"
2424

25-
"github.com/compose-spec/compose-go/types"
2625
"github.com/docker/compose/v2/pkg/utils"
2726
"github.com/sirupsen/logrus"
2827
"github.com/spf13/cobra"
@@ -79,15 +78,9 @@ func downCommand(p *projectOptions, backend api.Service) *cobra.Command {
7978
}
8079

8180
func runDown(ctx context.Context, backend api.Service, opts downOptions) error {
82-
name := opts.ProjectName
83-
var project *types.Project
84-
if opts.ProjectName == "" {
85-
p, err := opts.toProject(nil)
86-
if err != nil {
87-
return err
88-
}
89-
project = p
90-
name = p.Name
81+
project, name, err := opts.projectOrName()
82+
if err != nil {
83+
return err
9184
}
9285

9386
var timeout *time.Duration

cmd/compose/events.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ func eventsCommand(p *projectOptions, backend api.Service) *cobra.Command {
5151
}
5252

5353
func runEvents(ctx context.Context, backend api.Service, opts eventsOpts, services []string) error {
54-
project, err := opts.toProjectName()
54+
name, err := opts.toProjectName()
5555
if err != nil {
5656
return err
5757
}
5858

59-
return backend.Events(ctx, project, api.EventsOptions{
59+
return backend.Events(ctx, name, api.EventsOptions{
6060
Services: services,
6161
Consumer: func(event api.Event) error {
6262
if opts.json {

cmd/compose/kill.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ func killCommand(p *projectOptions, backend api.Service) *cobra.Command {
4949
}
5050

5151
func runKill(ctx context.Context, backend api.Service, opts killOptions, services []string) error {
52-
projectName, err := opts.toProjectName()
52+
name, err := opts.toProjectName()
5353
if err != nil {
5454
return err
5555
}
5656

57-
return backend.Kill(ctx, projectName, api.KillOptions{
57+
return backend.Kill(ctx, name, api.KillOptions{
5858
Services: services,
5959
Signal: opts.signal,
6060
})

cmd/compose/pause.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ func pauseCommand(p *projectOptions, backend api.Service) *cobra.Command {
4444
}
4545

4646
func runPause(ctx context.Context, backend api.Service, opts pauseOptions, services []string) error {
47-
project, err := opts.toProjectName()
47+
project, name, err := opts.projectOrName()
4848
if err != nil {
4949
return err
5050
}
5151

52-
return backend.Pause(ctx, project, api.PauseOptions{
52+
return backend.Pause(ctx, name, api.PauseOptions{
5353
Services: services,
54+
Project: project,
5455
})
5556
}
5657

@@ -74,12 +75,13 @@ func unpauseCommand(p *projectOptions, backend api.Service) *cobra.Command {
7475
}
7576

7677
func runUnPause(ctx context.Context, backend api.Service, opts unpauseOptions, services []string) error {
77-
project, err := opts.toProjectName()
78+
project, name, err := opts.projectOrName()
7879
if err != nil {
7980
return err
8081
}
8182

82-
return backend.UnPause(ctx, project, api.PauseOptions{
83+
return backend.UnPause(ctx, name, api.PauseOptions{
8384
Services: services,
85+
Project: project,
8486
})
8587
}

cmd/compose/remove.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,25 @@ Any data which is not in a volume will be lost.`,
5959
}
6060

6161
func runRemove(ctx context.Context, backend api.Service, opts removeOptions, services []string) error {
62-
project, err := opts.toProjectName()
62+
project, name, err := opts.projectOrName()
6363
if err != nil {
6464
return err
6565
}
6666

6767
if opts.stop {
68-
err := backend.Stop(ctx, project, api.StopOptions{
68+
err := backend.Stop(ctx, name, api.StopOptions{
6969
Services: services,
70+
Project: project,
7071
})
7172
if err != nil {
7273
return err
7374
}
7475
}
7576

76-
return backend.Remove(ctx, project, api.RemoveOptions{
77+
return backend.Remove(ctx, name, api.RemoveOptions{
7778
Services: services,
7879
Force: opts.force,
7980
Volumes: opts.volumes,
81+
Project: project,
8082
})
8183
}

cmd/compose/restart.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,15 @@ func restartCommand(p *projectOptions, backend api.Service) *cobra.Command {
4949
}
5050

5151
func runRestart(ctx context.Context, backend api.Service, opts restartOptions, services []string) error {
52-
projectName, err := opts.toProjectName()
52+
project, name, err := opts.projectOrName()
5353
if err != nil {
5454
return err
5555
}
5656

5757
timeout := time.Duration(opts.timeout) * time.Second
58-
return backend.Restart(ctx, projectName, api.RestartOptions{
58+
return backend.Restart(ctx, name, api.RestartOptions{
5959
Timeout: &timeout,
6060
Services: services,
61+
Project: project,
6162
})
6263
}

cmd/compose/start.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ func startCommand(p *projectOptions, backend api.Service) *cobra.Command {
4343
}
4444

4545
func runStart(ctx context.Context, backend api.Service, opts startOptions, services []string) error {
46-
projectName, err := opts.toProjectName()
46+
project, name, err := opts.projectOrName()
4747
if err != nil {
4848
return err
4949
}
5050

51-
return backend.Start(ctx, projectName, api.StartOptions{
51+
return backend.Start(ctx, name, api.StartOptions{
5252
AttachTo: services,
53+
Project: project,
5354
})
5455
}

cmd/compose/stop.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func stopCommand(p *projectOptions, backend api.Service) *cobra.Command {
5353
}
5454

5555
func runStop(ctx context.Context, backend api.Service, opts stopOptions, services []string) error {
56-
projectName, err := opts.toProjectName()
56+
project, name, err := opts.projectOrName()
5757
if err != nil {
5858
return err
5959
}
@@ -63,8 +63,9 @@ func runStop(ctx context.Context, backend api.Service, opts stopOptions, service
6363
timeoutValue := time.Duration(opts.timeout) * time.Second
6464
timeout = &timeoutValue
6565
}
66-
return backend.Stop(ctx, projectName, api.StopOptions{
66+
return backend.Stop(ctx, name, api.StopOptions{
6767
Timeout: timeout,
6868
Services: services,
69+
Project: project,
6970
})
7071
}

pkg/api/api.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ type CreateOptions struct {
117117

118118
// StartOptions group options of the Start API
119119
type StartOptions struct {
120-
// Project is the compose project used to define this app. Might be nil if user ran `start` just with project name
120+
// Project is the compose project used to define this app. Might be nil if user ran command just with project name
121121
Project *types.Project
122122
// Attach to container and forward logs if not nil
123123
Attach LogConsumer
@@ -133,6 +133,8 @@ type StartOptions struct {
133133

134134
// RestartOptions group options of the Restart API
135135
type RestartOptions struct {
136+
// Project is the compose project used to define this app. Might be nil if user ran command just with project name
137+
Project *types.Project
136138
// Timeout override container restart timeout
137139
Timeout *time.Duration
138140
// Services passed in the command line to be restarted
@@ -141,6 +143,8 @@ type RestartOptions struct {
141143

142144
// StopOptions group options of the Stop API
143145
type StopOptions struct {
146+
// Project is the compose project used to define this app. Might be nil if user ran command just with project name
147+
Project *types.Project
144148
// Timeout override container stop timeout
145149
Timeout *time.Duration
146150
// Services passed in the command line to be stopped
@@ -201,6 +205,8 @@ type KillOptions struct {
201205

202206
// RemoveOptions group options of the Remove API
203207
type RemoveOptions struct {
208+
// Project is the compose project used to define this app. Might be nil if user ran command just with project name
209+
Project *types.Project
204210
// DryRun just list removable resources
205211
DryRun bool
206212
// Volumes remove anonymous volumes
@@ -213,6 +219,8 @@ type RemoveOptions struct {
213219

214220
// RunOptions group options of the Run API
215221
type RunOptions struct {
222+
// Project is the compose project used to define this app. Might be nil if user ran command just with project name
223+
Project *types.Project
216224
Name string
217225
Service string
218226
Command []string
@@ -377,6 +385,8 @@ type LogOptions struct {
377385
type PauseOptions struct {
378386
// Services passed in the command line to be started
379387
Services []string
388+
// Project is the compose project used to define this app. Might be nil if user ran command just with project name
389+
Project *types.Project
380390
}
381391

382392
const (

0 commit comments

Comments
 (0)