Skip to content

Commit f622bce

Browse files
authored
Merge branch 'main' into fix/attach-error-handling
2 parents 27089c6 + ec88588 commit f622bce

File tree

10 files changed

+75
-12
lines changed

10 files changed

+75
-12
lines changed

cmd/compose/down.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func downCommand(p *ProjectOptions, dockerCli command.Cli, backendOptions *Backe
6060
RunE: Adapt(func(ctx context.Context, args []string) error {
6161
return runDown(ctx, dockerCli, backendOptions, opts, args)
6262
}),
63-
ValidArgsFunction: noCompletion(),
63+
ValidArgsFunction: completeServiceNames(dockerCli, p),
6464
}
6565
flags := downCmd.Flags()
6666
removeOrphans := utils.StringToBool(os.Getenv(ComposeRemoveOrphans))

cmd/compose/start.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ func runStart(ctx context.Context, dockerCli command.Cli, backendOptions *Backen
6363
return err
6464
}
6565

66-
timeout := time.Duration(opts.waitTimeout) * time.Second
66+
var timeout time.Duration
67+
if opts.waitTimeout > 0 {
68+
timeout = time.Duration(opts.waitTimeout) * time.Second
69+
}
6770
return backend.Start(ctx, name, api.StartOptions{
6871
AttachTo: services,
6972
Project: project,

cmd/compose/up.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ func upCommand(p *ProjectOptions, dockerCli command.Cli, backendOptions *Backend
188188

189189
//nolint:gocyclo
190190
func validateFlags(up *upOptions, create *createOptions) error {
191+
if up.waitTimeout < 0 {
192+
return fmt.Errorf("--wait-timeout must be a non-negative integer")
193+
}
191194
if up.exitCodeFrom != "" && !up.cascadeFail {
192195
up.cascadeStop = true
193196
}
@@ -328,7 +331,10 @@ func runUp(
328331
attach = attachSet.Elements()
329332
}
330333

331-
timeout := time.Duration(upOptions.waitTimeout) * time.Second
334+
var timeout time.Duration
335+
if upOptions.waitTimeout > 0 {
336+
timeout = time.Duration(upOptions.waitTimeout) * time.Second
337+
}
332338
return backend.Up(ctx, project, api.UpOptions{
333339
Create: create,
334340
Start: api.StartOptions{

cmd/compose/up_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121

2222
"github.com/compose-spec/compose-go/v2/types"
2323
"gotest.tools/v3/assert"
24+
25+
"github.com/docker/compose/v5/pkg/api"
2426
)
2527

2628
func TestApplyScaleOpt(t *testing.T) {
@@ -48,3 +50,42 @@ func TestApplyScaleOpt(t *testing.T) {
4850
assert.Equal(t, *bar.Scale, 3)
4951
assert.Equal(t, *bar.Deploy.Replicas, 3)
5052
}
53+
54+
func TestUpOptions_OnExit(t *testing.T) {
55+
tests := []struct {
56+
name string
57+
args upOptions
58+
want api.Cascade
59+
}{
60+
{
61+
name: "no cascade",
62+
args: upOptions{},
63+
want: api.CascadeIgnore,
64+
},
65+
{
66+
name: "cascade stop",
67+
args: upOptions{cascadeStop: true},
68+
want: api.CascadeStop,
69+
},
70+
{
71+
name: "cascade fail",
72+
args: upOptions{cascadeFail: true},
73+
want: api.CascadeFail,
74+
},
75+
{
76+
name: "both set - stop takes precedence",
77+
args: upOptions{
78+
cascadeStop: true,
79+
cascadeFail: true,
80+
},
81+
want: api.CascadeStop,
82+
},
83+
}
84+
85+
for _, tt := range tests {
86+
t.Run(tt.name, func(t *testing.T) {
87+
got := tt.args.OnExit()
88+
assert.Equal(t, got, tt.want)
89+
})
90+
}
91+
}

pkg/bridge/convert.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/docker/docker/api/types/network"
3636
"github.com/docker/docker/pkg/jsonmessage"
3737
"github.com/docker/go-connections/nat"
38+
"github.com/sirupsen/logrus"
3839
"go.yaml.in/yaml/v4"
3940

4041
"github.com/docker/compose/v5/pkg/api"
@@ -85,7 +86,17 @@ func convert(ctx context.Context, dockerCli command.Cli, model map[string]any, o
8586
return err
8687
}
8788

88-
dir := os.TempDir()
89+
dir, err := os.MkdirTemp("", "compose-convert-*")
90+
if err != nil {
91+
return err
92+
}
93+
defer func() {
94+
err := os.RemoveAll(dir)
95+
if err != nil {
96+
logrus.Warnf("failed to remove temp dir %s: %v", dir, err)
97+
}
98+
}()
99+
89100
composeYaml := filepath.Join(dir, "compose.yaml")
90101
err = os.WriteFile(composeYaml, raw, 0o600)
91102
if err != nil {

pkg/compose/build.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti
4040
return Run(ctx, func(ctx context.Context) error {
4141
return tracing.SpanWrapFunc("project/build", tracing.ProjectOptions(ctx, project),
4242
func(ctx context.Context) error {
43-
_, err := s.build(ctx, project, options, nil)
43+
builtImages, err := s.build(ctx, project, options, nil)
44+
if err == nil && len(builtImages) == 0 {
45+
logrus.Warn("No services to build")
46+
}
4447
return err
4548
})(ctx)
4649
}, "build", s.events)
@@ -91,7 +94,6 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
9194
}
9295

9396
if len(serviceToBuild) == 0 {
94-
logrus.Warn("No services to build")
9597
return imageIDs, nil
9698
}
9799

pkg/compose/compose.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ var swarmEnabled = struct {
478478
err error
479479
}{}
480480

481-
func (s *composeService) isSWarmEnabled(ctx context.Context) (bool, error) {
481+
func (s *composeService) isSwarmEnabled(ctx context.Context) (bool, error) {
482482
swarmEnabled.once.Do(func() {
483483
info, err := s.apiClient().Info(ctx)
484484
if err != nil {

pkg/compose/convergence.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func (c *convergence) ensureService(ctx context.Context, project *types.Project,
121121
actual := len(containers)
122122
updated := make(Containers, expected)
123123

124-
eg, _ := errgroup.WithContext(ctx)
124+
eg, ctx := errgroup.WithContext(ctx)
125125

126126
err = c.resolveServiceReferences(&service)
127127
if err != nil {
@@ -451,7 +451,7 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
451451
defer cancelFunc()
452452
ctx = withTimeout
453453
}
454-
eg, _ := errgroup.WithContext(ctx)
454+
eg, ctx := errgroup.WithContext(ctx)
455455
for dep, config := range dependencies {
456456
if shouldWait, err := shouldWaitForDependency(dep, config, project); err != nil {
457457
return err

pkg/compose/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1524,7 +1524,7 @@ func (s *composeService) resolveExternalNetwork(ctx context.Context, n *types.Ne
15241524
case 1:
15251525
return networks[0].ID, nil
15261526
case 0:
1527-
enabled, err := s.isSWarmEnabled(ctx)
1527+
enabled, err := s.isSwarmEnabled(ctx)
15281528
if err != nil {
15291529
return "", err
15301530
}

pkg/compose/down.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (s *composeService) down(ctx context.Context, projectName string, options a
119119
logrus.Warnf("Warning: No resource found to remove for project %q.", projectName)
120120
}
121121

122-
eg, _ := errgroup.WithContext(ctx)
122+
eg, ctx := errgroup.WithContext(ctx)
123123
for _, op := range ops {
124124
eg.Go(op)
125125
}
@@ -335,7 +335,7 @@ func (s *composeService) stopContainers(ctx context.Context, serv *types.Service
335335
}
336336

337337
func (s *composeService) removeContainers(ctx context.Context, containers []containerType.Summary, service *types.ServiceConfig, timeout *time.Duration, volumes bool) error {
338-
eg, _ := errgroup.WithContext(ctx)
338+
eg, ctx := errgroup.WithContext(ctx)
339339
for _, ctr := range containers {
340340
eg.Go(func() error {
341341
return s.stopAndRemoveContainer(ctx, ctr, service, timeout, volumes)

0 commit comments

Comments
 (0)