code cleanup: most progress.Run don't return a value

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2021-06-01 14:29:39 +02:00
parent 89a1a27f80
commit 9b0bc6fee1
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
14 changed files with 51 additions and 53 deletions

View File

@ -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

View File

@ -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),

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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

View File

@ -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
}

View File

@ -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
}

View File

@ -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

View File

@ -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 {

View File

@ -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