From 9b0bc6fee1c1669336ad819f6db20a0192cdd6bd Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Tue, 1 Jun 2021 14:29:39 +0200 Subject: [PATCH] code cleanup: most progress.Run don't return a value Signed-off-by: Nicolas De Loof --- api/progress/writer.go | 17 +++++++++++++---- cli/cmd/compose/build.go | 4 ++-- cli/cmd/compose/down.go | 7 +++---- cli/cmd/compose/pause.go | 9 ++++----- cli/cmd/compose/pull.go | 5 ++--- cli/cmd/compose/push.go | 5 ++--- cli/cmd/compose/remove.go | 18 ++++++++---------- cli/cmd/compose/restart.go | 5 ++--- cli/cmd/compose/run.go | 4 ++-- cli/cmd/compose/start.go | 5 ++--- cli/cmd/compose/stop.go | 5 ++--- cli/cmd/compose/up.go | 16 +++++++--------- cli/cmd/run/run.go | 2 +- cli/cmd/volume/command.go | 2 +- 14 files changed, 51 insertions(+), 53 deletions(-) diff --git a/api/progress/writer.go b/api/progress/writer.go index 7832366ad..172de52f9 100644 --- a/api/progress/writer.go +++ b/api/progress/writer.go @@ -50,11 +50,20 @@ func ContextWriter(ctx context.Context) Writer { return s } -type progressFunc func(context.Context) (string, error) +type progressFunc func(context.Context) error -// Run will run a writer and the progress function -// in parallel -func Run(ctx context.Context, pf progressFunc) (string, 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) error { + _, err := RunWithStatus(ctx, func(ctx context.Context) (string, error) { + return "", pf(ctx) + }) + return err +} + +// RunWithStatus will run a writer and the progress function in parallel and return a status +func RunWithStatus(ctx context.Context, pf progressFuncWithStatus) (string, error) { eg, _ := errgroup.WithContext(ctx) w, err := NewWriter(os.Stderr) var result string diff --git a/cli/cmd/compose/build.go b/cli/cmd/compose/build.go index c39421360..dbbbfbc89 100644 --- a/cli/cmd/compose/build.go +++ b/cli/cmd/compose/build.go @@ -88,8 +88,8 @@ func runBuild(ctx context.Context, backend compose.Service, opts buildOptions, s return err } - _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { - return "", backend.Build(ctx, project, compose.BuildOptions{ + err = progress.Run(ctx, func(ctx context.Context) error { + return backend.Build(ctx, project, compose.BuildOptions{ Pull: opts.pull, Progress: opts.progress, Args: types.NewMappingWithEquals(opts.args), diff --git a/cli/cmd/compose/down.go b/cli/cmd/compose/down.go index 3a3d0ba62..af42cfbdd 100644 --- a/cli/cmd/compose/down.go +++ b/cli/cmd/compose/down.go @@ -73,13 +73,13 @@ func downCommand(p *projectOptions, contextType string, backend compose.Service) } func runDown(ctx context.Context, backend compose.Service, opts downOptions) error { - _, err := progress.Run(ctx, func(ctx context.Context) (string, error) { + return progress.Run(ctx, func(ctx context.Context) error { name := opts.ProjectName var project *types.Project if opts.ProjectName == "" { p, err := opts.toProject(nil) if err != nil { - return "", err + return err } project = p name = p.Name @@ -90,7 +90,7 @@ func runDown(ctx context.Context, backend compose.Service, opts downOptions) err timeoutValue := time.Duration(opts.timeout) * time.Second timeout = &timeoutValue } - return name, backend.Down(ctx, name, compose.DownOptions{ + return backend.Down(ctx, name, compose.DownOptions{ RemoveOrphans: opts.removeOrphans, Project: project, Timeout: timeout, @@ -98,5 +98,4 @@ func runDown(ctx context.Context, backend compose.Service, opts downOptions) err Volumes: opts.volumes, }) }) - return err } diff --git a/cli/cmd/compose/pause.go b/cli/cmd/compose/pause.go index 13287f9a5..046c7c5bd 100644 --- a/cli/cmd/compose/pause.go +++ b/cli/cmd/compose/pause.go @@ -49,8 +49,8 @@ func runPause(ctx context.Context, backend compose.Service, opts pauseOptions, s return err } - _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { - return "", backend.Pause(ctx, project, compose.PauseOptions{ + err = progress.Run(ctx, func(ctx context.Context) error { + return backend.Pause(ctx, project, compose.PauseOptions{ Services: services, }) }) @@ -81,10 +81,9 @@ func runUnPause(ctx context.Context, backend compose.Service, opts unpauseOption return err } - _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { - return "", backend.UnPause(ctx, project, compose.PauseOptions{ + return progress.Run(ctx, func(ctx context.Context) error { + return backend.UnPause(ctx, project, compose.PauseOptions{ Services: services, }) }) - return err } diff --git a/cli/cmd/compose/pull.go b/cli/cmd/compose/pull.go index b944fed55..b7d7ebbef 100644 --- a/cli/cmd/compose/pull.go +++ b/cli/cmd/compose/pull.go @@ -94,8 +94,7 @@ func runPull(ctx context.Context, backend compose.Service, opts pullOptions, ser return backend.Pull(ctx, project, apiOpts) } - _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { - return "", backend.Pull(ctx, project, apiOpts) + return progress.Run(ctx, func(ctx context.Context) error { + return backend.Pull(ctx, project, apiOpts) }) - return err } diff --git a/cli/cmd/compose/push.go b/cli/cmd/compose/push.go index 91531793c..53903389f 100644 --- a/cli/cmd/compose/push.go +++ b/cli/cmd/compose/push.go @@ -54,10 +54,9 @@ func runPush(ctx context.Context, backend compose.Service, opts pushOptions, ser return err } - _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { - return "", backend.Push(ctx, project, compose.PushOptions{ + return progress.Run(ctx, func(ctx context.Context) error { + return backend.Push(ctx, project, compose.PushOptions{ IgnoreFailures: opts.Ignorefailures, }) }) - return err } diff --git a/cli/cmd/compose/remove.go b/cli/cmd/compose/remove.go index c949663e9..c9537231a 100644 --- a/cli/cmd/compose/remove.go +++ b/cli/cmd/compose/remove.go @@ -69,18 +69,17 @@ func runRemove(ctx context.Context, backend compose.Service, opts removeOptions, } if opts.stop { - _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { - err := backend.Stop(ctx, project, compose.StopOptions{ + err = progress.Run(ctx, func(ctx context.Context) error { + return backend.Stop(ctx, project, compose.StopOptions{ Services: services, }) - return "", err }) if err != nil { return err } } - reosurces, err := backend.Remove(ctx, project, compose.RemoveOptions{ + resources, err := backend.Remove(ctx, project, compose.RemoveOptions{ DryRun: true, Services: services, }) @@ -88,11 +87,11 @@ func runRemove(ctx context.Context, backend compose.Service, opts removeOptions, return err } - if len(reosurces) == 0 { + if len(resources) == 0 { fmt.Println("No stopped containers") return nil } - msg := fmt.Sprintf("Going to remove %s", strings.Join(reosurces, ", ")) + msg := fmt.Sprintf("Going to remove %s", strings.Join(resources, ", ")) if opts.force { fmt.Println(msg) } else { @@ -105,12 +104,11 @@ func runRemove(ctx context.Context, backend compose.Service, opts removeOptions, } } - _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { - _, err = backend.Remove(ctx, project, compose.RemoveOptions{ + return progress.Run(ctx, func(ctx context.Context) error { + _, err := backend.Remove(ctx, project, compose.RemoveOptions{ Volumes: opts.volumes, Force: opts.force, }) - return "", err + return err }) - return err } diff --git a/cli/cmd/compose/restart.go b/cli/cmd/compose/restart.go index 225b3ef46..fdfbfc998 100644 --- a/cli/cmd/compose/restart.go +++ b/cli/cmd/compose/restart.go @@ -55,11 +55,10 @@ func runRestart(ctx context.Context, backend compose.Service, opts restartOption } timeout := time.Duration(opts.timeout) * time.Second - _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { - return "", backend.Restart(ctx, project, compose.RestartOptions{ + return progress.Run(ctx, func(ctx context.Context) error { + return backend.Restart(ctx, project, compose.RestartOptions{ Timeout: &timeout, Services: services, }) }) - return err } diff --git a/cli/cmd/compose/run.go b/cli/cmd/compose/run.go index 651f48ec0..7d619a4db 100644 --- a/cli/cmd/compose/run.go +++ b/cli/cmd/compose/run.go @@ -154,8 +154,8 @@ func runRun(ctx context.Context, backend compose.Service, opts runOptions) error return err } - _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { - return "", startDependencies(ctx, backend, *project, opts.Service) + err = progress.Run(ctx, func(ctx context.Context) error { + return startDependencies(ctx, backend, *project, opts.Service) }) if err != nil { return err diff --git a/cli/cmd/compose/start.go b/cli/cmd/compose/start.go index 3e8b0b4e8..5e5b0f878 100644 --- a/cli/cmd/compose/start.go +++ b/cli/cmd/compose/start.go @@ -49,8 +49,7 @@ func runStart(ctx context.Context, backend compose.Service, opts startOptions, s return err } - _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { - return "", backend.Start(ctx, project, compose.StartOptions{}) + return progress.Run(ctx, func(ctx context.Context) error { + return backend.Start(ctx, project, compose.StartOptions{}) }) - return err } diff --git a/cli/cmd/compose/stop.go b/cli/cmd/compose/stop.go index 2a12a55ba..102c0735e 100644 --- a/cli/cmd/compose/stop.go +++ b/cli/cmd/compose/stop.go @@ -63,11 +63,10 @@ func runStop(ctx context.Context, backend compose.Service, opts stopOptions, ser timeoutValue := time.Duration(opts.timeout) * time.Second timeout = &timeoutValue } - _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { - return "", backend.Stop(ctx, project, compose.StopOptions{ + return progress.Run(ctx, func(ctx context.Context) error { + return backend.Stop(ctx, project, compose.StopOptions{ Timeout: timeout, Services: services, }) }) - return err } diff --git a/cli/cmd/compose/up.go b/cli/cmd/compose/up.go index 21465f64a..ec9e5dd3f 100644 --- a/cli/cmd/compose/up.go +++ b/cli/cmd/compose/up.go @@ -214,13 +214,12 @@ func runUp(ctx context.Context, backend compose.Service, opts upOptions, service return err } - _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { - return "", backend.Up(ctx, project, compose.UpOptions{ + return progress.Run(ctx, func(ctx context.Context) error { + return backend.Up(ctx, project, compose.UpOptions{ Detach: opts.Detach, QuietPull: opts.quietPull, }) }) - return err } func runCreateStart(ctx context.Context, backend compose.Service, opts upOptions, services []string) error { @@ -238,7 +237,7 @@ func runCreateStart(ctx context.Context, backend compose.Service, opts upOptions return fmt.Errorf("no service selected") } - _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { + err = progress.Run(ctx, func(ctx context.Context) error { err := backend.Create(ctx, project, compose.CreateOptions{ Services: services, RemoveOrphans: opts.removeOrphans, @@ -249,14 +248,14 @@ func runCreateStart(ctx context.Context, backend compose.Service, opts upOptions QuietPull: opts.quietPull, }) if err != nil { - return "", err + return err } if opts.Detach { err = backend.Start(ctx, project, compose.StartOptions{ Services: services, }) } - return "", err + return err }) if err != nil { return err @@ -282,15 +281,14 @@ func runCreateStart(ctx context.Context, backend compose.Service, opts upOptions stopFunc := func() error { ctx := context.Background() - _, err := progress.Run(ctx, func(ctx context.Context) (string, error) { + return progress.Run(ctx, func(ctx context.Context) error { go func() { <-signalChan backend.Kill(ctx, project, compose.KillOptions{}) // nolint:errcheck }() - return "", backend.Stop(ctx, project, compose.StopOptions{}) + return backend.Stop(ctx, project, compose.StopOptions{}) }) - return err } go func() { <-signalChan diff --git a/cli/cmd/run/run.go b/cli/cmd/run/run.go index 62ea4be89..b00e40546 100644 --- a/cli/cmd/run/run.go +++ b/cli/cmd/run/run.go @@ -103,7 +103,7 @@ func runRun(ctx context.Context, image string, contextType string, opts run.Opts return err } - result, err := progress.Run(ctx, func(ctx context.Context) (string, error) { + result, err := progress.RunWithStatus(ctx, func(ctx context.Context) (string, error) { return containerConfig.ID, c.ContainerService().Run(ctx, containerConfig) }) if err != nil { diff --git a/cli/cmd/volume/command.go b/cli/cmd/volume/command.go index 8188c3205..9b35b521c 100644 --- a/cli/cmd/volume/command.go +++ b/cli/cmd/volume/command.go @@ -74,7 +74,7 @@ func createVolume(ctype string) *cobra.Command { if err != nil { return err } - result, err := progress.Run(ctx, func(ctx context.Context) (string, error) { + result, err := progress.RunWithStatus(ctx, func(ctx context.Context) (string, error) { volume, err := c.VolumeService().Create(ctx, args[0], opts) if err != nil { return "", err