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

Commit 7a7114f

Browse files
committed
Capture container exit code and dump on console
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
1 parent 1562af9 commit 7a7114f

File tree

15 files changed

+124
-51
lines changed

15 files changed

+124
-51
lines changed

aci/compose.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (cs *aciComposeService) Create(ctx context.Context, project *types.Project,
6060
return errdefs.ErrNotImplemented
6161
}
6262

63-
func (cs *aciComposeService) Start(ctx context.Context, project *types.Project, consumer compose.LogConsumer) error {
63+
func (cs *aciComposeService) Start(ctx context.Context, project *types.Project, options compose.StartOptions) error {
6464
return errdefs.ErrNotImplemented
6565
}
6666

api/client/compose.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (c *composeService) Create(ctx context.Context, project *types.Project, opt
4444
return errdefs.ErrNotImplemented
4545
}
4646

47-
func (c *composeService) Start(ctx context.Context, project *types.Project, consumer compose.LogConsumer) error {
47+
func (c *composeService) Start(ctx context.Context, project *types.Project, options compose.StartOptions) error {
4848
return errdefs.ErrNotImplemented
4949
}
5050

api/compose/api.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type Service interface {
3434
// Create executes the equivalent to a `compose create`
3535
Create(ctx context.Context, project *types.Project, opts CreateOptions) error
3636
// Start executes the equivalent to a `compose start`
37-
Start(ctx context.Context, project *types.Project, consumer LogConsumer) error
37+
Start(ctx context.Context, project *types.Project, options StartOptions) error
3838
// Stop executes the equivalent to a `compose stop`
3939
Stop(ctx context.Context, project *types.Project) error
4040
// Up executes the equivalent to a `compose up`
@@ -63,6 +63,14 @@ type CreateOptions struct {
6363
Recreate string
6464
}
6565

66+
// StartOptions group options of the Start API
67+
type StartOptions struct {
68+
// Attach will attach to container and pipe stdout/stderr to LogConsumer
69+
Attach LogConsumer
70+
// CascadeStop will run `Stop` on any container exit
71+
CascadeStop bool
72+
}
73+
6674
// UpOptions group options of the Up API
6775
type UpOptions struct {
6876
// Detach will create services and return immediately
@@ -177,4 +185,5 @@ type Stack struct {
177185
// LogConsumer is a callback to process log messages from services
178186
type LogConsumer interface {
179187
Log(service, container, message string)
188+
Exit(service, container string, exitCode int)
180189
}

cli/cmd/compose/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func startDependencies(ctx context.Context, c *client.Client, project *types.Pro
102102
if err := c.ComposeService().Create(ctx, project, compose.CreateOptions{}); err != nil {
103103
return err
104104
}
105-
if err := c.ComposeService().Start(ctx, project, nil); err != nil {
105+
if err := c.ComposeService().Start(ctx, project, compose.StartOptions{}); err != nil {
106106
return err
107107
}
108108
return nil

cli/cmd/compose/start.go

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

1919
import (
2020
"context"
21+
"github.com/docker/compose-cli/api/compose"
2122
"os"
2223

2324
"github.com/spf13/cobra"
@@ -61,10 +62,12 @@ func runStart(ctx context.Context, opts startOptions, services []string) error {
6162

6263
if opts.Detach {
6364
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
64-
return "", c.ComposeService().Start(ctx, project, nil)
65+
return "", c.ComposeService().Start(ctx, project, compose.StartOptions{})
6566
})
6667
return err
6768
}
6869

69-
return c.ComposeService().Start(ctx, project, formatter.NewLogConsumer(ctx, os.Stdout))
70+
return c.ComposeService().Start(ctx, project, compose.StartOptions{
71+
Attach: formatter.NewLogConsumer(ctx, os.Stdout),
72+
})
7073
}

cli/cmd/compose/up.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
129129
return "", err
130130
}
131131
if opts.Detach {
132-
err = c.ComposeService().Start(ctx, project, nil)
132+
err = c.ComposeService().Start(ctx, project, compose.StartOptions{})
133133
}
134134
return "", err
135135
})
@@ -145,7 +145,9 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
145145
return nil
146146
}
147147

148-
err = c.ComposeService().Start(ctx, project, formatter.NewLogConsumer(ctx, os.Stdout))
148+
err = c.ComposeService().Start(ctx, project, compose.StartOptions{
149+
Attach: formatter.NewLogConsumer(ctx, os.Stdout),
150+
})
149151
if errors.Is(ctx.Err(), context.Canceled) {
150152
fmt.Println("Gracefully stopping...")
151153
ctx = context.Background()

cli/formatter/logs.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,7 @@ func (l *logConsumer) Log(service, container, message string) {
4242
if l.ctx.Err() != nil {
4343
return
4444
}
45-
cf, ok := l.colors[service]
46-
if !ok {
47-
cf = <-loop
48-
l.colors[service] = cf
49-
l.computeWidth()
50-
}
45+
cf := l.getColorFunc(service)
5146
prefix := fmt.Sprintf("%-"+strconv.Itoa(l.width)+"s |", container)
5247

5348
for _, line := range strings.Split(message, "\n") {
@@ -56,6 +51,21 @@ func (l *logConsumer) Log(service, container, message string) {
5651
}
5752
}
5853

54+
func (l *logConsumer) Exit(service, container string, exitCode int) {
55+
msg := fmt.Sprintf("%s exited with code %d\n", container, exitCode)
56+
l.writer.Write([]byte(l.getColorFunc(service)(msg)))
57+
}
58+
59+
func (l *logConsumer) getColorFunc(service string) colorFunc {
60+
cf, ok := l.colors[service]
61+
if !ok {
62+
cf = <-loop
63+
l.colors[service] = cf
64+
l.computeWidth()
65+
}
66+
return cf
67+
}
68+
5969
func (l *logConsumer) computeWidth() {
6070
width := 0
6171
for n := range l.colors {

ecs/local/compose.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ func (e ecsLocalSimulation) Create(ctx context.Context, project *types.Project,
5353
return e.compose.Create(ctx, enhanced, opts)
5454
}
5555

56-
func (e ecsLocalSimulation) Start(ctx context.Context, project *types.Project, consumer compose.LogConsumer) error {
57-
return e.compose.Start(ctx, project, consumer)
56+
func (e ecsLocalSimulation) Start(ctx context.Context, project *types.Project, options compose.StartOptions) error {
57+
return e.compose.Start(ctx, project, options)
5858
}
5959

6060
func (e ecsLocalSimulation) Stop(ctx context.Context, project *types.Project) error {

ecs/logs.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,9 @@ func (a *allowListLogConsumer) Log(service, container, message string) {
5454
a.delegate.Log(service, container, message)
5555
}
5656
}
57+
58+
func (a *allowListLogConsumer) Exit(service, container string, exitCode int) {
59+
if a.allowList[service] {
60+
a.delegate.Exit(service, container, exitCode)
61+
}
62+
}

ecs/up.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (b *ecsAPIService) Create(ctx context.Context, project *types.Project, opts
4747
return errdefs.ErrNotImplemented
4848
}
4949

50-
func (b *ecsAPIService) Start(ctx context.Context, project *types.Project, consumer compose.LogConsumer) error {
50+
func (b *ecsAPIService) Start(ctx context.Context, project *types.Project, options compose.StartOptions) error {
5151
return errdefs.ErrNotImplemented
5252
}
5353

0 commit comments

Comments
 (0)