Browse Source

progress: make title configurable (#10507)

Signed-off-by: Guillaume Lours <[email protected]>
Guillaume Lours 2 years ago
parent
commit
03f4c0e631

+ 2 - 2
pkg/compose/build.go

@@ -47,10 +47,10 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti
 	if err != nil {
 		return err
 	}
-	return progress.Run(ctx, func(ctx context.Context) error {
+	return progress.RunWithTitle(ctx, func(ctx context.Context) error {
 		_, err := s.build(ctx, project, options)
 		return err
-	}, s.stderr())
+	}, s.stderr(), "Building")
 }
 
 //nolint:gocyclo

+ 2 - 2
pkg/compose/cp.go

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

+ 2 - 2
pkg/compose/create.go

@@ -49,9 +49,9 @@ import (
 )
 
 func (s *composeService) Create(ctx context.Context, project *types.Project, options api.CreateOptions) error {
-	return progress.Run(ctx, func(ctx context.Context) error {
+	return progress.RunWithTitle(ctx, func(ctx context.Context) error {
 		return s.create(ctx, project, options)
-	}, s.stderr())
+	}, s.stderr(), "Creating")
 }
 
 func (s *composeService) create(ctx context.Context, project *types.Project, options api.CreateOptions) 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.Run(ctx, func(ctx context.Context) error {
+	return progress.RunWithTitle(ctx, func(ctx context.Context) error {
 		return s.kill(ctx, strings.ToLower(projectName), options)
-	}, s.stderr())
+	}, s.stderr(), "Killing")
 }
 
 func (s *composeService) kill(ctx context.Context, projectName string, options api.KillOptions) error {

+ 2 - 2
pkg/compose/pause.go

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

+ 2 - 2
pkg/compose/pull.go

@@ -42,9 +42,9 @@ func (s *composeService) Pull(ctx context.Context, project *types.Project, optio
 	if options.Quiet {
 		return s.pull(ctx, project, options)
 	}
-	return progress.Run(ctx, func(ctx context.Context) error {
+	return progress.RunWithTitle(ctx, func(ctx context.Context) error {
 		return s.pull(ctx, project, options)
-	}, s.stderr())
+	}, s.stderr(), "Pulling")
 }
 
 func (s *composeService) pull(ctx context.Context, project *types.Project, opts api.PullOptions) error { //nolint:gocyclo

+ 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.Run(ctx, func(ctx context.Context) error {
+	return progress.RunWithTitle(ctx, func(ctx context.Context) error {
 		return s.push(ctx, project, options)
-	}, s.stderr())
+	}, s.stderr(), "Pushing")
 }
 
 func (s *composeService) push(ctx context.Context, project *types.Project, options api.PushOptions) error {

+ 2 - 2
pkg/compose/remove.go

@@ -93,9 +93,9 @@ func (s *composeService) Remove(ctx context.Context, projectName string, options
 			return nil
 		}
 	}
-	return progress.Run(ctx, func(ctx context.Context) error {
+	return progress.RunWithTitle(ctx, func(ctx context.Context) error {
 		return s.remove(ctx, stoppedContainers, options)
-	}, s.stderr())
+	}, s.stderr(), "Removing")
 }
 
 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.Run(ctx, func(ctx context.Context) error {
+	return progress.RunWithTitle(ctx, func(ctx context.Context) error {
 		return s.restart(ctx, strings.ToLower(projectName), options)
-	}, s.stderr())
+	}, s.stderr(), "Restarting")
 }
 
 func (s *composeService) restart(ctx context.Context, projectName string, options api.RestartOptions) 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.Run(ctx, func(ctx context.Context) error {
+	return progress.RunWithTitle(ctx, func(ctx context.Context) error {
 		return s.stop(ctx, strings.ToLower(projectName), options)
-	}, s.stderr())
+	}, s.stderr(), "Stopping")
 }
 
 func (s *composeService) stop(ctx context.Context, projectName string, options api.StopOptions) error {

+ 2 - 1
pkg/progress/tty.go

@@ -43,6 +43,7 @@ type ttyWriter struct {
 	tailEvents      []string
 	dryRun          bool
 	skipChildEvents bool
+	progressTitle   string
 }
 
 func (w *ttyWriter) Start(ctx context.Context) error {
@@ -149,7 +150,7 @@ func (w *ttyWriter) print() { //nolint:gocyclo
 	fmt.Fprint(w.out, aec.Hide)
 	defer fmt.Fprint(w.out, aec.Show)
 
-	firstLine := fmt.Sprintf("[+] Running %d/%d", numDone(w.events), w.numLines)
+	firstLine := fmt.Sprintf("[+] %s %d/%d", w.progressTitle, numDone(w.events), w.numLines)
 	if w.numLines != 0 && numDone(w.events) == w.numLines {
 		firstLine = DoneColor(firstLine)
 	}

+ 22 - 14
pkg/progress/writer.go

@@ -61,14 +61,21 @@ type progressFuncWithStatus func(context.Context) (string, error)
 func Run(ctx context.Context, pf progressFunc, out io.Writer) error {
 	_, err := RunWithStatus(ctx, func(ctx context.Context) (string, error) {
 		return "", pf(ctx)
-	}, out)
+	}, out, "Running")
+	return err
+}
+
+func RunWithTitle(ctx context.Context, pf progressFunc, out io.Writer, 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 io.Writer) (string, error) {
+func RunWithStatus(ctx context.Context, pf progressFuncWithStatus, out io.Writer, progressTitle string) (string, error) {
 	eg, _ := errgroup.WithContext(ctx)
-	w, err := NewWriter(ctx, out)
+	w, err := NewWriter(ctx, out, progressTitle)
 	var result string
 	if err != nil {
 		return "", err
@@ -105,17 +112,17 @@ const (
 var Mode = ModeAuto
 
 // NewWriter returns a new multi-progress writer
-func NewWriter(ctx context.Context, out io.Writer) (Writer, error) {
+func NewWriter(ctx context.Context, out io.Writer, progressTitle string) (Writer, error) {
 	_, isTerminal := term.GetFdInfo(out)
 	dryRun, ok := ctx.Value(api.DryRunKey{}).(bool)
 	if !ok {
 		dryRun = false
 	}
 	if Mode == ModeAuto && isTerminal {
-		return newTTYWriter(out.(console.File), dryRun)
+		return newTTYWriter(out.(console.File), dryRun, progressTitle)
 	}
 	if Mode == ModeTTY {
-		return newTTYWriter(out.(console.File), dryRun)
+		return newTTYWriter(out.(console.File), dryRun, progressTitle)
 	}
 	return &plainWriter{
 		out:    out,
@@ -124,19 +131,20 @@ func NewWriter(ctx context.Context, out io.Writer) (Writer, error) {
 	}, nil
 }
 
-func newTTYWriter(out console.File, dryRun bool) (Writer, error) {
+func newTTYWriter(out console.File, dryRun bool, progressTitle string) (Writer, error) {
 	con, err := console.ConsoleFromFile(out)
 	if err != nil {
 		return nil, err
 	}
 
 	return &ttyWriter{
-		out:      con,
-		eventIDs: []string{},
-		events:   map[string]Event{},
-		repeated: false,
-		done:     make(chan bool),
-		mtx:      &sync.Mutex{},
-		dryRun:   dryRun,
+		out:           con,
+		eventIDs:      []string{},
+		events:        map[string]Event{},
+		repeated:      false,
+		done:          make(chan bool),
+		mtx:           &sync.Mutex{},
+		dryRun:        dryRun,
+		progressTitle: progressTitle,
 	}, nil
 }