Jelajahi Sumber

Introduce ergonomic API for handling multiple container events

Signed-off-by: Nikhil Benesch <[email protected]>
Nikhil Benesch 4 tahun lalu
induk
melakukan
ee586e7f1e
5 mengubah file dengan 29 tambahan dan 13 penghapusan
  1. 12 12
      pkg/compose/convergence.go
  2. 4 1
      pkg/progress/noop.go
  3. 6 0
      pkg/progress/plain.go
  4. 6 0
      pkg/progress/tty.go
  5. 1 0
      pkg/progress/writer.go

+ 12 - 12
pkg/compose/convergence.go

@@ -261,6 +261,14 @@ func getContainerProgressName(container moby.Container) string {
 	return "Container " + getCanonicalContainerName(container)
 	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
 // ServiceConditionRunningOrHealthy is a service condition on statys running or healthy
 const ServiceConditionRunningOrHealthy = "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 {
 		if err != nil {
 			return err
 			return err
 		}
 		}
-		for _, container := range containers {
-			w.Event(progress.Waiting(getContainerProgressName(container)))
-		}
+		w.Events(containerEvents(containers, progress.Waiting))
 
 
 		dep, config := dep, config
 		dep, config := dep, config
 		eg.Go(func() error {
 		eg.Go(func() error {
@@ -294,9 +300,7 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
 						return err
 						return err
 					}
 					}
 					if healthy {
 					if healthy {
-						for _, container := range containers {
-							w.Event(progress.Healthy(getContainerProgressName(container)))
-						}
+						w.Events(containerEvents(containers, progress.Healthy))
 						return nil
 						return nil
 					}
 					}
 				case types.ServiceConditionHealthy:
 				case types.ServiceConditionHealthy:
@@ -305,9 +309,7 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
 						return err
 						return err
 					}
 					}
 					if healthy {
 					if healthy {
-						for _, container := range containers {
-							w.Event(progress.Healthy(getContainerProgressName(container)))
-						}
+						w.Events(containerEvents(containers, progress.Healthy))
 						return nil
 						return nil
 					}
 					}
 				case types.ServiceConditionCompletedSuccessfully:
 				case types.ServiceConditionCompletedSuccessfully:
@@ -316,9 +318,7 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
 						return err
 						return err
 					}
 					}
 					if exited {
 					if exited {
-						for _, container := range containers {
-							w.Event(progress.Exited(getContainerProgressName(container)))
-						}
+						w.Events(containerEvents(containers, progress.Exited))
 						if code != 0 {
 						if code != 0 {
 							return fmt.Errorf("service %q didn't completed successfully: exit %d", dep, code)
 							return fmt.Errorf("service %q didn't completed successfully: exit %d", dep, code)
 						}
 						}

+ 4 - 1
pkg/progress/noop.go

@@ -27,7 +27,10 @@ func (p *noopWriter) Start(ctx context.Context) error {
 	return nil
 	return nil
 }
 }
 
 
-func (p *noopWriter) Event(e Event) {
+func (p *noopWriter) Event(Event) {
+}
+
+func (p *noopWriter) Events([]Event) {
 }
 }
 
 
 func (p *noopWriter) TailMsgf(_ string, _ ...interface{}) {
 func (p *noopWriter) TailMsgf(_ string, _ ...interface{}) {

+ 6 - 0
pkg/progress/plain.go

@@ -40,6 +40,12 @@ func (p *plainWriter) Event(e Event) {
 	fmt.Fprintln(p.out, e.ID, e.Text, e.StatusText)
 	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{}) {
 func (p *plainWriter) TailMsgf(m string, args ...interface{}) {
 	fmt.Fprintln(p.out, append([]interface{}{m}, args...)...)
 	fmt.Fprintln(p.out, append([]interface{}{m}, args...)...)
 }
 }

+ 6 - 0
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{}) {
 func (w *ttyWriter) TailMsgf(msg string, args ...interface{}) {
 	w.mtx.Lock()
 	w.mtx.Lock()
 	defer w.mtx.Unlock()
 	defer w.mtx.Unlock()

+ 1 - 0
pkg/progress/writer.go

@@ -31,6 +31,7 @@ type Writer interface {
 	Start(context.Context) error
 	Start(context.Context) error
 	Stop()
 	Stop()
 	Event(Event)
 	Event(Event)
+	Events([]Event)
 	TailMsgf(string, ...interface{})
 	TailMsgf(string, ...interface{})
 }
 }