Răsfoiți Sursa

remove unused RunWithStatus, always pass operation as title

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 2 luni în urmă
părinte
comite
ae25d27e5a

+ 4 - 7
pkg/compose/build.go

@@ -58,13 +58,13 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti
 	if err != nil {
 		return err
 	}
-	return progress.RunWithTitle(ctx, func(ctx context.Context) error {
+	return progress.Run(ctx, func(ctx context.Context) error {
 		return tracing.SpanWrapFunc("project/build", tracing.ProjectOptions(ctx, project),
 			func(ctx context.Context) error {
 				_, err := s.build(ctx, project, options, nil)
 				return err
 			})(ctx)
-	}, s.stdinfo(), "Building")
+	}, s.stdinfo(), "build")
 }
 
 //nolint:gocyclo
@@ -204,16 +204,12 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
 		if !ok {
 			return nil
 		}
-		serviceName := fmt.Sprintf("Service %s", name)
-
 		if !buildkitEnabled {
 			trace.SpanFromContext(ctx).SetAttributes(attribute.String("builder", "classic"))
-			s.events(ctx, progress.BuildingEvent(serviceName))
 			id, err := s.doBuildClassic(ctx, project, service, options)
 			if err != nil {
 				return err
 			}
-			s.events(ctx, progress.BuiltEvent(serviceName))
 			builtDigests[getServiceIndex(name)] = id
 
 			if options.Push {
@@ -230,6 +226,7 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
 		if err != nil {
 			return err
 		}
+		s.events(ctx, progress.BuildingEvent("Image "+buildOptions.Tags[0]))
 
 		trace.SpanFromContext(ctx).SetAttributes(attribute.String("builder", "buildkit"))
 		digest, err := s.doBuildBuildkit(ctx, name, buildOptions, w, nodes)
@@ -259,7 +256,7 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
 			service := project.Services[names[i]]
 			imageRef := api.GetImageNameOrDefault(service, project.Name)
 			imageIDs[imageRef] = imageDigest
-			s.events(ctx, progress.BuiltEvent(names[i]))
+			s.events(ctx, progress.BuiltEvent("Image "+imageRef))
 		}
 	}
 	return imageIDs, err

+ 1 - 1
pkg/compose/build_bake.go

@@ -426,7 +426,7 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
 			return nil, fmt.Errorf("build result not found in Bake metadata for service %s", name)
 		}
 		results[image] = built.Digest
-		s.events(ctx, progress.BuiltEvent(image))
+		s.events(ctx, progress.BuiltEvent("Image "+image))
 	}
 	return results, nil
 }

+ 3 - 0
pkg/compose/build_classic.go

@@ -30,6 +30,7 @@ import (
 	"github.com/docker/cli/cli"
 	"github.com/docker/cli/cli/command/image/build"
 	"github.com/docker/compose/v2/pkg/api"
+	progress2 "github.com/docker/compose/v2/pkg/progress"
 	buildtypes "github.com/docker/docker/api/types/build"
 	"github.com/docker/docker/api/types/container"
 	"github.com/docker/docker/api/types/registry"
@@ -183,6 +184,7 @@ func (s *composeService) doBuildClassic(ctx context.Context, project *types.Proj
 
 	ctx, cancel := context.WithCancel(ctx)
 	defer cancel()
+	s.events(ctx, progress2.BuildingEvent("Image "+imageName))
 	response, err := s.apiClient().ImageBuild(ctx, body, buildOpts)
 	if err != nil {
 		return "", err
@@ -211,6 +213,7 @@ func (s *composeService) doBuildClassic(ctx context.Context, project *types.Proj
 		}
 		return "", err
 	}
+	s.events(ctx, progress2.BuiltEvent("Image "+imageName))
 	return imageID, nil
 }
 

+ 2 - 2
pkg/compose/commit.go

@@ -27,9 +27,9 @@ import (
 )
 
 func (s *composeService) Commit(ctx context.Context, projectName string, options api.CommitOptions) error {
-	return progress.RunWithTitle(ctx, func(ctx context.Context) error {
+	return progress.Run(ctx, func(ctx context.Context) error {
 		return s.commit(ctx, projectName, options)
-	}, s.stdinfo(), "Committing")
+	}, s.stdinfo(), "commit")
 }
 
 func (s *composeService) commit(ctx context.Context, projectName string, options api.CommitOptions) error {

+ 2 - 2
pkg/compose/cp.go

@@ -43,9 +43,9 @@ const (
 )
 
 func (s *composeService) Copy(ctx context.Context, projectName string, options api.CopyOptions) error {
-	return progress.RunWithTitle(ctx, func(ctx context.Context) error {
+	return progress.Run(ctx, func(ctx context.Context) error {
 		return s.copy(ctx, projectName, options)
-	}, s.stdinfo(), "Copying")
+	}, s.stdinfo(), "copy")
 }
 
 func (s *composeService) copy(ctx context.Context, projectName string, options api.CopyOptions) error {

+ 2 - 2
pkg/compose/create.go

@@ -61,9 +61,9 @@ type createConfigs struct {
 }
 
 func (s *composeService) Create(ctx context.Context, project *types.Project, createOpts api.CreateOptions) error {
-	return progress.RunWithTitle(ctx, func(ctx context.Context) error {
+	return progress.Run(ctx, func(ctx context.Context) error {
 		return s.create(ctx, project, createOpts)
-	}, s.stdinfo(), "Creating")
+	}, s.stdinfo(), "create")
 }
 
 func (s *composeService) create(ctx context.Context, project *types.Project, options api.CreateOptions) error {

+ 1 - 1
pkg/compose/down.go

@@ -40,7 +40,7 @@ type downOp func() error
 func (s *composeService) Down(ctx context.Context, projectName string, options api.DownOptions) error {
 	return progress.Run(ctx, func(ctx context.Context) error {
 		return s.down(ctx, strings.ToLower(projectName), options)
-	}, s.stdinfo())
+	}, s.stdinfo(), "down")
 }
 
 func (s *composeService) down(ctx context.Context, projectName string, options api.DownOptions) error { //nolint:gocyclo

+ 2 - 2
pkg/compose/export.go

@@ -29,9 +29,9 @@ import (
 )
 
 func (s *composeService) Export(ctx context.Context, projectName string, options api.ExportOptions) error {
-	return progress.RunWithTitle(ctx, func(ctx context.Context) error {
+	return progress.Run(ctx, func(ctx context.Context) error {
 		return s.export(ctx, projectName, options)
-	}, s.stdinfo(), "Exporting")
+	}, s.stdinfo(), "export")
 }
 
 func (s *composeService) export(ctx context.Context, projectName string, options api.ExportOptions) error {

+ 2 - 2
pkg/compose/kill.go

@@ -29,9 +29,9 @@ import (
 )
 
 func (s *composeService) Kill(ctx context.Context, projectName string, options api.KillOptions) error {
-	return progress.RunWithTitle(ctx, func(ctx context.Context) error {
+	return progress.Run(ctx, func(ctx context.Context) error {
 		return s.kill(ctx, strings.ToLower(projectName), options)
-	}, s.stdinfo(), "Killing")
+	}, s.stdinfo(), "kill")
 }
 
 func (s *composeService) kill(ctx context.Context, projectName string, options api.KillOptions) error {

+ 3 - 3
pkg/compose/pause.go

@@ -28,9 +28,9 @@ import (
 )
 
 func (s *composeService) Pause(ctx context.Context, projectName string, options api.PauseOptions) error {
-	return progress.RunWithTitle(ctx, func(ctx context.Context) error {
+	return progress.Run(ctx, func(ctx context.Context) error {
 		return s.pause(ctx, strings.ToLower(projectName), options)
-	}, s.stdinfo(), "Pausing")
+	}, s.stdinfo(), "pause")
 }
 
 func (s *composeService) pause(ctx context.Context, projectName string, options api.PauseOptions) error {
@@ -60,7 +60,7 @@ func (s *composeService) pause(ctx context.Context, projectName string, options
 func (s *composeService) UnPause(ctx context.Context, projectName string, options api.PauseOptions) error {
 	return progress.Run(ctx, func(ctx context.Context) error {
 		return s.unPause(ctx, strings.ToLower(projectName), options)
-	}, s.stdinfo())
+	}, s.stdinfo(), "unpause")
 }
 
 func (s *composeService) unPause(ctx context.Context, projectName string, options api.PauseOptions) error {

+ 2 - 2
pkg/compose/publish.go

@@ -43,9 +43,9 @@ import (
 )
 
 func (s *composeService) Publish(ctx context.Context, project *types.Project, repository string, options api.PublishOptions) error {
-	return progress.RunWithTitle(ctx, func(ctx context.Context) error {
+	return progress.Run(ctx, func(ctx context.Context) error {
 		return s.publish(ctx, project, repository, options)
-	}, s.stdinfo(), "Publishing")
+	}, s.stdinfo(), "publish")
 }
 
 //nolint:gocyclo

+ 28 - 30
pkg/compose/pull.go

@@ -44,9 +44,9 @@ import (
 )
 
 func (s *composeService) Pull(ctx context.Context, project *types.Project, options api.PullOptions) error {
-	return progress.RunWithTitle(ctx, func(ctx context.Context) error {
+	return progress.Run(ctx, func(ctx context.Context) error {
 		return s.pull(ctx, project, options)
-	}, s.stdinfo(), "Pulling")
+	}, s.stdinfo(), "pull")
 }
 
 func (s *composeService) pull(ctx context.Context, project *types.Project, opts api.PullOptions) error { //nolint:gocyclo
@@ -320,36 +320,34 @@ func (s *composeService) pullRequiredImages(ctx context.Context, project *types.
 		return nil
 	}
 
-	return progress.Run(ctx, func(ctx context.Context) error {
-		eg, ctx := errgroup.WithContext(ctx)
-		eg.SetLimit(s.maxConcurrency)
-		pulledImages := map[string]api.ImageSummary{}
-		var mutex sync.Mutex
-		for name, service := range needPull {
-			eg.Go(func() error {
-				id, err := s.pullServiceImage(ctx, service, quietPull, project.Environment["DOCKER_DEFAULT_PLATFORM"])
-				mutex.Lock()
-				defer mutex.Unlock()
-				pulledImages[name] = api.ImageSummary{
-					ID:          id,
-					Repository:  service.Image,
-					LastTagTime: time.Now(),
-				}
-				if err != nil && isServiceImageToBuild(service, project.Services) {
-					// image can be built, so we can ignore pull failure
-					return nil
-				}
-				return err
-			})
-		}
-		err := eg.Wait()
-		for i, service := range needPull {
-			if pulledImages[i].ID != "" {
-				images[service.Image] = pulledImages[i]
+	eg, ctx := errgroup.WithContext(ctx)
+	eg.SetLimit(s.maxConcurrency)
+	pulledImages := map[string]api.ImageSummary{}
+	var mutex sync.Mutex
+	for name, service := range needPull {
+		eg.Go(func() error {
+			id, err := s.pullServiceImage(ctx, service, quietPull, project.Environment["DOCKER_DEFAULT_PLATFORM"])
+			mutex.Lock()
+			defer mutex.Unlock()
+			pulledImages[name] = api.ImageSummary{
+				ID:          id,
+				Repository:  service.Image,
+				LastTagTime: time.Now(),
+			}
+			if err != nil && isServiceImageToBuild(service, project.Services) {
+				// image can be built, so we can ignore pull failure
+				return nil
 			}
+			return err
+		})
+	}
+	err := eg.Wait()
+	for i, service := range needPull {
+		if pulledImages[i].ID != "" {
+			images[service.Image] = pulledImages[i]
 		}
-		return err
-	}, s.stdinfo())
+	}
+	return err
 }
 
 func mustPull(service types.ServiceConfig, images map[string]api.ImageSummary) (bool, error) {

+ 2 - 2
pkg/compose/push.go

@@ -40,9 +40,9 @@ func (s *composeService) Push(ctx context.Context, project *types.Project, optio
 	if options.Quiet {
 		return s.push(ctx, project, options)
 	}
-	return progress.RunWithTitle(ctx, func(ctx context.Context) error {
+	return progress.Run(ctx, func(ctx context.Context) error {
 		return s.push(ctx, project, options)
-	}, s.stdinfo(), "Pushing")
+	}, s.stdinfo(), "push")
 }
 
 func (s *composeService) push(ctx context.Context, project *types.Project, options api.PushOptions) error {

+ 2 - 2
pkg/compose/remove.go

@@ -92,9 +92,9 @@ func (s *composeService) Remove(ctx context.Context, projectName string, options
 			return nil
 		}
 	}
-	return progress.RunWithTitle(ctx, func(ctx context.Context) error {
+	return progress.Run(ctx, func(ctx context.Context) error {
 		return s.remove(ctx, stoppedContainers, options)
-	}, s.stdinfo(), "Removing")
+	}, s.stdinfo(), "remove")
 }
 
 func (s *composeService) remove(ctx context.Context, containers Containers, options api.RemoveOptions) error {

+ 2 - 2
pkg/compose/restart.go

@@ -29,9 +29,9 @@ import (
 )
 
 func (s *composeService) Restart(ctx context.Context, projectName string, options api.RestartOptions) error {
-	return progress.RunWithTitle(ctx, func(ctx context.Context) error {
+	return progress.Run(ctx, func(ctx context.Context) error {
 		return s.restart(ctx, strings.ToLower(projectName), options)
-	}, s.stdinfo(), "Restarting")
+	}, s.stdinfo(), "restart")
 }
 
 func (s *composeService) restart(ctx context.Context, projectName string, options api.RestartOptions) error { //nolint:gocyclo

+ 1 - 1
pkg/compose/run.go

@@ -67,7 +67,7 @@ func (s *composeService) prepareRun(ctx context.Context, project *types.Project,
 
 	err = progress.Run(ctx, func(ctx context.Context) error {
 		return s.startDependencies(ctx, project, opts)
-	}, s.stdinfo())
+	}, s.stdinfo(), "run")
 	if err != nil {
 		return "", err
 	}

+ 1 - 1
pkg/compose/scale.go

@@ -31,5 +31,5 @@ func (s *composeService) Scale(ctx context.Context, project *types.Project, opti
 			return err
 		}
 		return s.start(ctx, project.Name, api.StartOptions{Project: project, Services: options.Services}, nil)
-	}), s.stdinfo())
+	}), s.stdinfo(), "scale")
 }

+ 1 - 1
pkg/compose/start.go

@@ -33,7 +33,7 @@ import (
 func (s *composeService) Start(ctx context.Context, projectName string, options api.StartOptions) error {
 	return progress.Run(ctx, func(ctx context.Context) error {
 		return s.start(ctx, strings.ToLower(projectName), options, nil)
-	}, s.stdinfo())
+	}, s.stdinfo(), "start")
 }
 
 func (s *composeService) start(ctx context.Context, projectName string, options api.StartOptions, listener api.ContainerEventListener) error {

+ 2 - 2
pkg/compose/stop.go

@@ -26,9 +26,9 @@ import (
 )
 
 func (s *composeService) Stop(ctx context.Context, projectName string, options api.StopOptions) error {
-	return progress.RunWithTitle(ctx, func(ctx context.Context) error {
+	return progress.Run(ctx, func(ctx context.Context) error {
 		return s.stop(ctx, strings.ToLower(projectName), options, nil)
-	}, s.stdinfo(), "Stopping")
+	}, s.stdinfo(), "stop")
 }
 
 func (s *composeService) stop(ctx context.Context, projectName string, options api.StopOptions, event api.ContainerEventListener) error {

+ 1 - 1
pkg/compose/up.go

@@ -49,7 +49,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
 			return s.start(ctx, project.Name, options.Start, nil)
 		}
 		return nil
-	}), s.stdinfo())
+	}), s.stdinfo(), "up")
 	if err != nil {
 		return err
 	}

+ 4 - 28
pkg/progress/writer.go

@@ -55,16 +55,6 @@ func ContextWriter(ctx context.Context) Writer {
 
 type progressFunc func(context.Context) error
 
-type progressFuncWithStatus func(context.Context) (string, error)
-
-// Run will run a writer and the progress function in parallel
-func Run(ctx context.Context, pf progressFunc, out *streams.Out) error {
-	_, err := RunWithStatus(ctx, func(ctx context.Context) (string, error) {
-		return "", pf(ctx)
-	}, out, "Running")
-	return err
-}
-
 func RunWithLog(ctx context.Context, pf progressFunc, out *streams.Out, logConsumer api.LogConsumer) error {
 	w := NewMixedWriter(out, logConsumer, false) // FIXME(ndeloof) re-implement dry-run
 	eg, _ := errgroup.WithContext(ctx)
@@ -80,20 +70,11 @@ func RunWithLog(ctx context.Context, pf progressFunc, out *streams.Out, logConsu
 	return eg.Wait()
 }
 
-func RunWithTitle(ctx context.Context, pf progressFunc, out *streams.Out, progressTitle string) error {
-	_, err := RunWithStatus(ctx, func(ctx context.Context) (string, error) {
-		return "", pf(ctx)
-	}, out, progressTitle)
-	return err
-}
-
-// RunWithStatus will run a writer and the progress function in parallel and return a status
-func RunWithStatus(ctx context.Context, pf progressFuncWithStatus, out *streams.Out, progressTitle string) (string, error) {
+func Run(ctx context.Context, pf progressFunc, out *streams.Out, progressTitle string) error {
 	eg, _ := errgroup.WithContext(ctx)
 	w, err := NewWriter(ctx, out, progressTitle)
-	var result string
 	if err != nil {
-		return "", err
+		return err
 	}
 	eg.Go(func() error {
 		return w.Start(context.Background())
@@ -103,15 +84,10 @@ func RunWithStatus(ctx context.Context, pf progressFuncWithStatus, out *streams.
 
 	eg.Go(func() error {
 		defer w.Stop()
-		s, err := pf(ctx)
-		if err == nil {
-			result = s
-		}
+		err := pf(ctx)
 		return err
 	})
-
-	err = eg.Wait()
-	return result, err
+	return eg.Wait()
 }
 
 const (