From 3252c409f8e181ed553a286c5973045c66b1718e Mon Sep 17 00:00:00 2001
From: Guillaume Tardif <guillaume.tardif@gmail.com>
Date: Fri, 29 Jan 2021 14:23:15 +0100
Subject: [PATCH] Make progress event display homogeneous between up, down,
 start, stop

Signed-off-by: Guillaume Tardif <guillaume.tardif@gmail.com>
---
 local/compose/convergence.go | 24 +++++++++++++++---------
 local/compose/down.go        |  8 +++++---
 2 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/local/compose/convergence.go b/local/compose/convergence.go
index c56adfa93..3b3fcc5df 100644
--- a/local/compose/convergence.go
+++ b/local/compose/convergence.go
@@ -98,7 +98,7 @@ func (s *composeService) ensureService(ctx context.Context, observedState Contai
 	}
 
 	for _, container := range actual {
-		name := getCanonicalContainerName(container)
+		name := getContainerProgressName(container)
 
 		diverged := container.Labels[configHashLabel] != expected
 		if diverged || recreate == compose.RecreateForce || service.Extensions[extLifecycle] == forceRecreate {
@@ -132,6 +132,10 @@ func getContainerName(projectName string, service types.ServiceConfig, number in
 	return name
 }
 
+func getContainerProgressName(container moby.Container) string {
+	return "Container " + getCanonicalContainerName(container)
+}
+
 func (s *composeService) waitDependencies(ctx context.Context, project *types.Project, service types.ServiceConfig) error {
 	eg, _ := errgroup.WithContext(ctx)
 	for dep, config := range service.DependsOn {
@@ -191,18 +195,19 @@ func getScale(config types.ServiceConfig) (int, error) {
 
 func (s *composeService) createContainer(ctx context.Context, project *types.Project, service types.ServiceConfig, name string, number int, autoRemove bool) error {
 	w := progress.ContextWriter(ctx)
-	w.Event(progress.CreatingEvent(name))
+	eventName := "Container " + name
+	w.Event(progress.CreatingEvent(eventName))
 	err := s.createMobyContainer(ctx, project, service, name, number, nil, autoRemove)
 	if err != nil {
 		return err
 	}
-	w.Event(progress.CreatedEvent(name))
+	w.Event(progress.CreatedEvent(eventName))
 	return nil
 }
 
 func (s *composeService) recreateContainer(ctx context.Context, project *types.Project, service types.ServiceConfig, container moby.Container) error {
 	w := progress.ContextWriter(ctx)
-	w.Event(progress.NewEvent(getCanonicalContainerName(container), progress.Working, "Recreate"))
+	w.Event(progress.NewEvent(getContainerProgressName(container), progress.Working, "Recreate"))
 	err := s.apiClient.ContainerStop(ctx, container.ID, nil)
 	if err != nil {
 		return err
@@ -225,7 +230,7 @@ func (s *composeService) recreateContainer(ctx context.Context, project *types.P
 	if err != nil {
 		return err
 	}
-	w.Event(progress.NewEvent(getCanonicalContainerName(container), progress.Done, "Recreated"))
+	w.Event(progress.NewEvent(getContainerProgressName(container), progress.Done, "Recreated"))
 	setDependentLifecycle(project, service.Name, forceRecreate)
 	return nil
 }
@@ -245,12 +250,12 @@ func setDependentLifecycle(project *types.Project, service string, strategy stri
 
 func (s *composeService) restartContainer(ctx context.Context, container moby.Container) error {
 	w := progress.ContextWriter(ctx)
-	w.Event(progress.NewEvent(getCanonicalContainerName(container), progress.Working, "Restart"))
+	w.Event(progress.NewEvent(getContainerProgressName(container), progress.Working, "Restart"))
 	err := s.apiClient.ContainerStart(ctx, container.ID, moby.ContainerStartOptions{})
 	if err != nil {
 		return err
 	}
-	w.Event(progress.NewEvent(getCanonicalContainerName(container), progress.Done, "Restarted"))
+	w.Event(progress.NewEvent(getContainerProgressName(container), progress.Done, "Restarted"))
 	return nil
 }
 
@@ -336,10 +341,11 @@ func (s *composeService) startService(ctx context.Context, project *types.Projec
 		}
 		eg.Go(func() error {
 			w := progress.ContextWriter(ctx)
-			w.Event(progress.StartingEvent(getCanonicalContainerName(container)))
+			eventName := getContainerProgressName(container)
+			w.Event(progress.StartingEvent(eventName))
 			err := s.apiClient.ContainerStart(ctx, container.ID, moby.ContainerStartOptions{})
 			if err == nil {
-				w.Event(progress.StartedEvent(getCanonicalContainerName(container)))
+				w.Event(progress.StartedEvent(eventName))
 			}
 			return err
 		})
diff --git a/local/compose/down.go b/local/compose/down.go
index 4a5448864..e769653e3 100644
--- a/local/compose/down.go
+++ b/local/compose/down.go
@@ -92,7 +92,7 @@ func (s *composeService) Down(ctx context.Context, projectName string, options c
 func (s *composeService) stopContainers(ctx context.Context, w progress.Writer, containers []moby.Container) error {
 	for _, container := range containers {
 		toStop := container
-		eventName := "Container " + getCanonicalContainerName(toStop)
+		eventName := getContainerProgressName(toStop)
 		w.Event(progress.StoppingEvent(eventName))
 		err := s.apiClient.ContainerStop(ctx, toStop.ID, nil)
 		if err != nil {
@@ -109,9 +109,11 @@ func (s *composeService) removeContainers(ctx context.Context, w progress.Writer
 	for _, container := range containers {
 		toDelete := container
 		eg.Go(func() error {
-			eventName := "Container " + getCanonicalContainerName(toDelete)
-			err := s.stopContainers(ctx, w, []moby.Container{container})
+			eventName := getContainerProgressName(toDelete)
+			w.Event(progress.StoppingEvent(eventName))
+			err := s.stopContainers(ctx, w, []moby.Container{toDelete})
 			if err != nil {
+				w.Event(progress.ErrorMessageEvent(eventName, "Error while Stopping"))
 				return err
 			}
 			w.Event(progress.RemovingEvent(eventName))