From ee586e7f1eea400cf722048f6db02696fcd73062 Mon Sep 17 00:00:00 2001 From: Nikhil Benesch Date: Fri, 7 Jan 2022 13:01:59 -0500 Subject: [PATCH] Introduce ergonomic API for handling multiple container events Signed-off-by: Nikhil Benesch --- pkg/compose/convergence.go | 24 ++++++++++++------------ pkg/progress/noop.go | 5 ++++- pkg/progress/plain.go | 6 ++++++ pkg/progress/tty.go | 6 ++++++ pkg/progress/writer.go | 1 + 5 files changed, 29 insertions(+), 13 deletions(-) diff --git a/pkg/compose/convergence.go b/pkg/compose/convergence.go index 401abbe48..24f051ee2 100644 --- a/pkg/compose/convergence.go +++ b/pkg/compose/convergence.go @@ -261,6 +261,14 @@ func getContainerProgressName(container moby.Container) string { return "Container " + getCanonicalContainerName(container) } +func containerEvents(containers Containers, eventFunc func(string) progress.Event) []progress.Event { + events := []progress.Event{} + for _, container := range containers { + events = append(events, eventFunc(getContainerProgressName(container))) + } + return events +} + // ServiceConditionRunningOrHealthy is a service condition on statys running or healthy const ServiceConditionRunningOrHealthy = "running_or_healthy" @@ -277,9 +285,7 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr if err != nil { return err } - for _, container := range containers { - w.Event(progress.Waiting(getContainerProgressName(container))) - } + w.Events(containerEvents(containers, progress.Waiting)) dep, config := dep, config eg.Go(func() error { @@ -294,9 +300,7 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr return err } if healthy { - for _, container := range containers { - w.Event(progress.Healthy(getContainerProgressName(container))) - } + w.Events(containerEvents(containers, progress.Healthy)) return nil } case types.ServiceConditionHealthy: @@ -305,9 +309,7 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr return err } if healthy { - for _, container := range containers { - w.Event(progress.Healthy(getContainerProgressName(container))) - } + w.Events(containerEvents(containers, progress.Healthy)) return nil } case types.ServiceConditionCompletedSuccessfully: @@ -316,9 +318,7 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr return err } if exited { - for _, container := range containers { - w.Event(progress.Exited(getContainerProgressName(container))) - } + w.Events(containerEvents(containers, progress.Exited)) if code != 0 { return fmt.Errorf("service %q didn't completed successfully: exit %d", dep, code) } diff --git a/pkg/progress/noop.go b/pkg/progress/noop.go index 46ec1290e..98852705c 100644 --- a/pkg/progress/noop.go +++ b/pkg/progress/noop.go @@ -27,7 +27,10 @@ func (p *noopWriter) Start(ctx context.Context) error { return nil } -func (p *noopWriter) Event(e Event) { +func (p *noopWriter) Event(Event) { +} + +func (p *noopWriter) Events([]Event) { } func (p *noopWriter) TailMsgf(_ string, _ ...interface{}) { diff --git a/pkg/progress/plain.go b/pkg/progress/plain.go index 98e26abbd..3524074da 100644 --- a/pkg/progress/plain.go +++ b/pkg/progress/plain.go @@ -40,6 +40,12 @@ func (p *plainWriter) Event(e Event) { fmt.Fprintln(p.out, e.ID, e.Text, e.StatusText) } +func (p *plainWriter) Events(events []Event) { + for _, e := range events { + p.Event(e) + } +} + func (p *plainWriter) TailMsgf(m string, args ...interface{}) { fmt.Fprintln(p.out, append([]interface{}{m}, args...)...) } diff --git a/pkg/progress/tty.go b/pkg/progress/tty.go index 5fe4270b9..25cb7c789 100644 --- a/pkg/progress/tty.go +++ b/pkg/progress/tty.go @@ -95,6 +95,12 @@ func (w *ttyWriter) Event(e Event) { } } +func (w *ttyWriter) Events(events []Event) { + for _, e := range events { + w.Event(e) + } +} + func (w *ttyWriter) TailMsgf(msg string, args ...interface{}) { w.mtx.Lock() defer w.mtx.Unlock() diff --git a/pkg/progress/writer.go b/pkg/progress/writer.go index 11d4044bb..352ecb030 100644 --- a/pkg/progress/writer.go +++ b/pkg/progress/writer.go @@ -31,6 +31,7 @@ type Writer interface { Start(context.Context) error Stop() Event(Event) + Events([]Event) TailMsgf(string, ...interface{}) }