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

Commit 752edcc

Browse files
committed
introduce --exit-code-from
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
1 parent a4b003e commit 752edcc

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

cli/cmd/compose/start.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func runStart(ctx context.Context, opts startOptions, services []string) error {
7676
return err
7777
}
7878

79-
_, err = printer.run(ctx, false, func() error {
79+
_, err = printer.run(ctx, false, "", func() error {
8080
ctx := context.Background()
8181
_, err := progress.Run(ctx, func(ctx context.Context) (string, error) {
8282
return "", c.ComposeService().Stop(ctx, project)

cli/cmd/compose/up.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ type upOptions struct {
5252
noRecreate bool
5353
noStart bool
5454
cascadeStop bool
55+
exitCodeFrom string
5556
}
5657

5758
func (o upOptions) recreateStrategy() string {
@@ -76,6 +77,9 @@ func upCommand(p *projectOptions, contextType string) *cobra.Command {
7677
RunE: func(cmd *cobra.Command, args []string) error {
7778
switch contextType {
7879
case store.LocalContextType, store.DefaultContextType, store.EcsLocalSimulationContextType:
80+
if opts.exitCodeFrom != "" {
81+
opts.cascadeStop = true
82+
}
7983
if opts.cascadeStop && opts.Detach {
8084
return fmt.Errorf("--abort-on-container-exit and --detach are incompatible")
8185
}
@@ -102,6 +106,7 @@ func upCommand(p *projectOptions, contextType string) *cobra.Command {
102106
flags.BoolVar(&opts.noRecreate, "no-recreate", false, "If containers already exist, don't recreate them. Incompatible with --force-recreate.")
103107
flags.BoolVar(&opts.noStart, "no-start", false, "Don't start the services after creating them.")
104108
flags.BoolVar(&opts.cascadeStop, "abort-on-container-exit", false, "Stops all containers if any container was stopped. Incompatible with -d")
109+
flags.StringVar(&opts.exitCodeFrom, "exit-code-from", "", "Return the exit code of the selected service container. Implies --abort-on-container-exit")
105110
}
106111

107112
return upCmd
@@ -179,7 +184,7 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
179184
return err
180185
}
181186

182-
_, err = printer.run(ctx, opts.cascadeStop, stopFunc)
187+
_, err = printer.run(ctx, opts.cascadeStop, opts.exitCodeFrom, stopFunc)
183188
// FIXME os.Exit
184189
return err
185190
}
@@ -229,21 +234,32 @@ type printer struct {
229234
queue chan compose.ContainerEvent
230235
}
231236

232-
func (p printer) run(ctx context.Context, cascadeStop bool, stopFn func() error) (int, error) { //nolint:unparam
237+
func (p printer) run(ctx context.Context, cascadeStop bool, exitCodeFrom string, stopFn func() error) (int, error) { //nolint:unparam
233238
consumer := formatter.NewLogConsumer(ctx, os.Stdout)
239+
var aborting bool
234240
for {
235241
event := <-p.queue
236242
switch event.Type {
237243
case compose.ContainerEventExit:
238-
consumer.Status(event.Service, event.Source, fmt.Sprintf("exited with code %d", event.ExitCode))
239-
if cascadeStop {
244+
if !aborting {
245+
consumer.Status(event.Service, event.Source, fmt.Sprintf("exited with code %d", event.ExitCode))
246+
}
247+
if cascadeStop && !aborting {
248+
aborting = true
240249
fmt.Println("Aborting on container exit...")
241250
err := stopFn()
251+
if err != nil {
252+
return 0, err
253+
}
254+
}
255+
if exitCodeFrom == "" || exitCodeFrom == event.Service {
242256
logrus.Error(event.ExitCode)
243-
return event.ExitCode, err
257+
return event.ExitCode, nil
244258
}
245259
case compose.ContainerEventLog:
246-
consumer.Log(event.Service, event.Source, event.Line)
260+
if !aborting {
261+
consumer.Log(event.Service, event.Source, event.Line)
262+
}
247263
}
248264
}
249265
}

local/compose/start.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func (s *composeService) Start(ctx context.Context, project *types.Project, opti
5656
options.Attach <- compose.ContainerEvent{
5757
Type: compose.ContainerEventExit,
5858
Source: getCanonicalContainerName(c),
59+
Service: c.Labels[serviceLabel],
5960
ExitCode: int(status.StatusCode),
6061
}
6162
case err := <-errC:

0 commit comments

Comments
 (0)