Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1730 from ndeloof/progress_cleanup
Browse files Browse the repository at this point in the history
code cleanup: most progress.Run don't return a value
  • Loading branch information
ndeloof authored Jun 2, 2021
2 parents 907e8e1 + 9b0bc6f commit 3ec33fa
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 53 deletions.
17 changes: 13 additions & 4 deletions api/progress/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cli/cmd/compose/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
7 changes: 3 additions & 4 deletions cli/cmd/compose/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -90,13 +90,12 @@ 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,
Images: opts.images,
Volumes: opts.volumes,
})
})
return err
}
9 changes: 4 additions & 5 deletions cli/cmd/compose/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
})
Expand Down Expand Up @@ -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
}
5 changes: 2 additions & 3 deletions cli/cmd/compose/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 2 additions & 3 deletions cli/cmd/compose/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
18 changes: 8 additions & 10 deletions cli/cmd/compose/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,30 +69,29 @@ 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,
})
if err != nil {
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 {
Expand All @@ -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
}
5 changes: 2 additions & 3 deletions cli/cmd/compose/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions cli/cmd/compose/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions cli/cmd/compose/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 2 additions & 3 deletions cli/cmd/compose/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
16 changes: 7 additions & 9 deletions cli/cmd/compose/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/volume/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3ec33fa

Please sign in to comment.