Skip to content

Commit ee586e7

Browse files
beneschndeloof
authored andcommitted
Introduce ergonomic API for handling multiple container events
Signed-off-by: Nikhil Benesch <[email protected]>
1 parent 5eb314a commit ee586e7

File tree

5 files changed

+29
-13
lines changed

5 files changed

+29
-13
lines changed

pkg/compose/convergence.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,14 @@ func getContainerProgressName(container moby.Container) string {
261261
return "Container " + getCanonicalContainerName(container)
262262
}
263263

264+
func containerEvents(containers Containers, eventFunc func(string) progress.Event) []progress.Event {
265+
events := []progress.Event{}
266+
for _, container := range containers {
267+
events = append(events, eventFunc(getContainerProgressName(container)))
268+
}
269+
return events
270+
}
271+
264272
// ServiceConditionRunningOrHealthy is a service condition on statys running or healthy
265273
const ServiceConditionRunningOrHealthy = "running_or_healthy"
266274

@@ -277,9 +285,7 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
277285
if err != nil {
278286
return err
279287
}
280-
for _, container := range containers {
281-
w.Event(progress.Waiting(getContainerProgressName(container)))
282-
}
288+
w.Events(containerEvents(containers, progress.Waiting))
283289

284290
dep, config := dep, config
285291
eg.Go(func() error {
@@ -294,9 +300,7 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
294300
return err
295301
}
296302
if healthy {
297-
for _, container := range containers {
298-
w.Event(progress.Healthy(getContainerProgressName(container)))
299-
}
303+
w.Events(containerEvents(containers, progress.Healthy))
300304
return nil
301305
}
302306
case types.ServiceConditionHealthy:
@@ -305,9 +309,7 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
305309
return err
306310
}
307311
if healthy {
308-
for _, container := range containers {
309-
w.Event(progress.Healthy(getContainerProgressName(container)))
310-
}
312+
w.Events(containerEvents(containers, progress.Healthy))
311313
return nil
312314
}
313315
case types.ServiceConditionCompletedSuccessfully:
@@ -316,9 +318,7 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
316318
return err
317319
}
318320
if exited {
319-
for _, container := range containers {
320-
w.Event(progress.Exited(getContainerProgressName(container)))
321-
}
321+
w.Events(containerEvents(containers, progress.Exited))
322322
if code != 0 {
323323
return fmt.Errorf("service %q didn't completed successfully: exit %d", dep, code)
324324
}

pkg/progress/noop.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ func (p *noopWriter) Start(ctx context.Context) error {
2727
return nil
2828
}
2929

30-
func (p *noopWriter) Event(e Event) {
30+
func (p *noopWriter) Event(Event) {
31+
}
32+
33+
func (p *noopWriter) Events([]Event) {
3134
}
3235

3336
func (p *noopWriter) TailMsgf(_ string, _ ...interface{}) {

pkg/progress/plain.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ func (p *plainWriter) Event(e Event) {
4040
fmt.Fprintln(p.out, e.ID, e.Text, e.StatusText)
4141
}
4242

43+
func (p *plainWriter) Events(events []Event) {
44+
for _, e := range events {
45+
p.Event(e)
46+
}
47+
}
48+
4349
func (p *plainWriter) TailMsgf(m string, args ...interface{}) {
4450
fmt.Fprintln(p.out, append([]interface{}{m}, args...)...)
4551
}

pkg/progress/tty.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ func (w *ttyWriter) Event(e Event) {
9595
}
9696
}
9797

98+
func (w *ttyWriter) Events(events []Event) {
99+
for _, e := range events {
100+
w.Event(e)
101+
}
102+
}
103+
98104
func (w *ttyWriter) TailMsgf(msg string, args ...interface{}) {
99105
w.mtx.Lock()
100106
defer w.mtx.Unlock()

pkg/progress/writer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type Writer interface {
3131
Start(context.Context) error
3232
Stop()
3333
Event(Event)
34+
Events([]Event)
3435
TailMsgf(string, ...interface{})
3536
}
3637

0 commit comments

Comments
 (0)