@@ -52,6 +52,7 @@ type upOptions struct {
5252 noRecreate bool
5353 noStart bool
5454 cascadeStop bool
55+ exitCodeFrom string
5556}
5657
5758func (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}
0 commit comments