Skip to content

Commit 46d936c

Browse files
committed
support attach
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 8a64ab5 commit 46d936c

File tree

5 files changed

+32
-12
lines changed

5 files changed

+32
-12
lines changed

cmd/compose/compose.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"strings"
2727
"syscall"
2828

29+
"github.com/compose-spec/compose-go/dotenv"
2930
buildx "github.com/docker/buildx/util/progress"
3031
"github.com/docker/cli/cli/command"
3132

@@ -472,7 +473,7 @@ func setEnvWithDotEnv(prjOpts *ProjectOptions) error {
472473
return err
473474
}
474475

475-
envFromFile, err := cli.GetEnvFromFile(composegoutils.GetAsEqualsMap(os.Environ()), workingDir, options.EnvFiles)
476+
envFromFile, err := dotenv.GetEnvFromFile(composegoutils.GetAsEqualsMap(os.Environ()), workingDir, options.EnvFiles)
476477
if err != nil {
477478
return err
478479
}

cmd/compose/up.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ import (
2121
"fmt"
2222
"time"
2323

24-
"github.com/docker/compose/v2/cmd/formatter"
25-
2624
"github.com/compose-spec/compose-go/types"
25+
"github.com/docker/compose/v2/cmd/formatter"
2726
"github.com/spf13/cobra"
2827

2928
"github.com/docker/compose/v2/pkg/api"
@@ -164,17 +163,31 @@ func runUp(ctx context.Context, streams api.Streams, backend api.Service, create
164163
consumer = formatter.NewLogConsumer(ctx, streams.Out(), streams.Err(), !upOptions.noColor, !upOptions.noPrefix, upOptions.timestamp)
165164
}
166165

167-
attachTo := services
166+
attachTo := utils.Set[string]{}
168167
if len(upOptions.attach) > 0 {
169-
attachTo = upOptions.attach
168+
attachTo.AddAll(upOptions.attach...)
170169
}
171170
if upOptions.attachDependencies {
172-
attachTo = project.ServiceNames()
171+
if err := project.WithServices(attachTo.Elements(), func(s types.ServiceConfig) error {
172+
if s.Attach == nil || *s.Attach {
173+
attachTo.Add(s.Name)
174+
}
175+
return nil
176+
}); err != nil {
177+
return err
178+
}
173179
}
174180
if len(attachTo) == 0 {
175-
attachTo = project.ServiceNames()
181+
if err := project.WithServices(services, func(s types.ServiceConfig) error {
182+
if s.Attach == nil || *s.Attach {
183+
attachTo.Add(s.Name)
184+
}
185+
return nil
186+
}); err != nil {
187+
return err
188+
}
176189
}
177-
attachTo = utils.Remove(attachTo, upOptions.noAttach...)
190+
attachTo.RemoveAll(upOptions.noAttach...)
178191

179192
create := api.CreateOptions{
180193
Services: services,
@@ -198,7 +211,7 @@ func runUp(ctx context.Context, streams api.Streams, backend api.Service, create
198211
Start: api.StartOptions{
199212
Project: project,
200213
Attach: consumer,
201-
AttachTo: attachTo,
214+
AttachTo: attachTo.Elements(),
202215
ExitCodeFrom: upOptions.exitCodeFrom,
203216
CascadeStop: upOptions.cascadeStop,
204217
Wait: upOptions.wait,

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/AlecAivazis/survey/v2 v2.3.7
77
github.com/Microsoft/go-winio v0.6.1
88
github.com/buger/goterm v1.0.4
9-
github.com/compose-spec/compose-go v1.15.1
9+
github.com/compose-spec/compose-go v1.16.0
1010
github.com/containerd/console v1.0.3
1111
github.com/containerd/containerd v1.7.2
1212
github.com/cucumber/godog v0.0.0-00010101000000-000000000000 // replaced; see replace for the actual version used

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWH
131131
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
132132
github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4 h1:/inchEIKaYC1Akx+H+gqO04wryn5h75LSazbRlnya1k=
133133
github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb h1:EDmT6Q9Zs+SbUoc7Ik9EfrFqcylYqgPZ9ANSbTAntnE=
134-
github.com/compose-spec/compose-go v1.15.1 h1:0yaEt6/66dLN0bNWYDTj0CDx626uCdQ9ipJVIJx8O8M=
135-
github.com/compose-spec/compose-go v1.15.1/go.mod h1:3yngGBGfls6FHGQsg4B1z6gz8ej9SOvmAJtxCwgbcnc=
134+
github.com/compose-spec/compose-go v1.16.0 h1:HYk4uYWXgArHh6NG+WE4yGYayOXw+hjqJ+eJxpjWWjk=
135+
github.com/compose-spec/compose-go v1.16.0/go.mod h1:3yngGBGfls6FHGQsg4B1z6gz8ej9SOvmAJtxCwgbcnc=
136136
github.com/containerd/cgroups v1.1.0 h1:v8rEWFl6EoqHB+swVNjVoCJE8o3jX7e8nqBGPLaDFBM=
137137
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
138138
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=

pkg/utils/set.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,9 @@ func (s Set[T]) Elements() []T {
4747
}
4848
return elements
4949
}
50+
51+
func (s Set[T]) RemoveAll(elements ...T) {
52+
for _, e := range elements {
53+
s.Remove(e)
54+
}
55+
}

0 commit comments

Comments
 (0)