Skip to content

Commit 150f27b

Browse files
committed
cli/command/container: use errors.Join
Use stdlib multi-errors instead of creating our own Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 791e06b commit 150f27b

8 files changed

Lines changed: 67 additions & 84 deletions

File tree

cli/command/container/kill.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ package container
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
6-
"strings"
77

88
"github.com/docker/cli/cli"
99
"github.com/docker/cli/cli/command"
1010
"github.com/docker/cli/cli/command/completion"
11-
"github.com/pkg/errors"
1211
"github.com/spf13/cobra"
1312
)
1413

@@ -44,20 +43,19 @@ func NewKillCommand(dockerCli command.Cli) *cobra.Command {
4443
return cmd
4544
}
4645

47-
func runKill(ctx context.Context, dockerCli command.Cli, opts *killOptions) error {
48-
var errs []string
46+
func runKill(ctx context.Context, dockerCLI command.Cli, opts *killOptions) error {
47+
apiClient := dockerCLI.Client()
4948
errChan := parallelOperation(ctx, opts.containers, func(ctx context.Context, container string) error {
50-
return dockerCli.Client().ContainerKill(ctx, container, opts.signal)
49+
return apiClient.ContainerKill(ctx, container, opts.signal)
5150
})
51+
52+
var errs []error
5253
for _, name := range opts.containers {
5354
if err := <-errChan; err != nil {
54-
errs = append(errs, err.Error())
55-
} else {
56-
_, _ = fmt.Fprintln(dockerCli.Out(), name)
55+
errs = append(errs, err)
56+
continue
5757
}
58+
_, _ = fmt.Fprintln(dockerCLI.Out(), name)
5859
}
59-
if len(errs) > 0 {
60-
return errors.New(strings.Join(errs, "\n"))
61-
}
62-
return nil
60+
return errors.Join(errs...)
6361
}

cli/command/container/pause.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package container
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
6-
"strings"
77

88
"github.com/docker/cli/cli"
99
"github.com/docker/cli/cli/command"
1010
"github.com/docker/cli/cli/command/completion"
1111
"github.com/docker/docker/api/types/container"
12-
"github.com/pkg/errors"
1312
"github.com/spf13/cobra"
1413
)
1514

@@ -38,18 +37,17 @@ func NewPauseCommand(dockerCli command.Cli) *cobra.Command {
3837
}
3938
}
4039

41-
func runPause(ctx context.Context, dockerCli command.Cli, opts *pauseOptions) error {
42-
var errs []string
43-
errChan := parallelOperation(ctx, opts.containers, dockerCli.Client().ContainerPause)
40+
func runPause(ctx context.Context, dockerCLI command.Cli, opts *pauseOptions) error {
41+
apiClient := dockerCLI.Client()
42+
errChan := parallelOperation(ctx, opts.containers, apiClient.ContainerPause)
43+
44+
var errs []error
4445
for _, ctr := range opts.containers {
4546
if err := <-errChan; err != nil {
46-
errs = append(errs, err.Error())
47+
errs = append(errs, err)
4748
continue
4849
}
49-
_, _ = fmt.Fprintln(dockerCli.Out(), ctr)
50-
}
51-
if len(errs) > 0 {
52-
return errors.New(strings.Join(errs, "\n"))
50+
_, _ = fmt.Fprintln(dockerCLI.Out(), ctr)
5351
}
54-
return nil
52+
return errors.Join(errs...)
5553
}

cli/command/container/restart.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package container
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
6-
"strings"
77

88
"github.com/docker/cli/cli"
99
"github.com/docker/cli/cli/command"
1010
"github.com/docker/cli/cli/command/completion"
1111
"github.com/docker/docker/api/types/container"
12-
"github.com/pkg/errors"
1312
"github.com/spf13/cobra"
1413
)
1514

@@ -56,27 +55,25 @@ func NewRestartCommand(dockerCli command.Cli) *cobra.Command {
5655
return cmd
5756
}
5857

59-
func runRestart(ctx context.Context, dockerCli command.Cli, opts *restartOptions) error {
60-
var errs []string
58+
func runRestart(ctx context.Context, dockerCLI command.Cli, opts *restartOptions) error {
6159
var timeout *int
6260
if opts.timeoutChanged {
6361
timeout = &opts.timeout
6462
}
6563

64+
apiClient := dockerCLI.Client()
65+
var errs []error
6666
// TODO(thaJeztah): consider using parallelOperation for restart, similar to "stop" and "remove"
6767
for _, name := range opts.containers {
68-
err := dockerCli.Client().ContainerRestart(ctx, name, container.StopOptions{
68+
err := apiClient.ContainerRestart(ctx, name, container.StopOptions{
6969
Signal: opts.signal,
7070
Timeout: timeout,
7171
})
7272
if err != nil {
73-
errs = append(errs, err.Error())
73+
errs = append(errs, err)
7474
continue
7575
}
76-
_, _ = fmt.Fprintln(dockerCli.Out(), name)
76+
_, _ = fmt.Fprintln(dockerCLI.Out(), name)
7777
}
78-
if len(errs) > 0 {
79-
return errors.New(strings.Join(errs, "\n"))
80-
}
81-
return nil
78+
return errors.Join(errs...)
8279
}

cli/command/container/rm.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package container
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"strings"
78

@@ -10,7 +11,6 @@ import (
1011
"github.com/docker/cli/cli/command/completion"
1112
"github.com/docker/docker/api/types/container"
1213
"github.com/docker/docker/errdefs"
13-
"github.com/pkg/errors"
1414
"github.com/spf13/cobra"
1515
)
1616

@@ -50,33 +50,31 @@ func NewRmCommand(dockerCli command.Cli) *cobra.Command {
5050
return cmd
5151
}
5252

53-
func runRm(ctx context.Context, dockerCli command.Cli, opts *rmOptions) error {
54-
var errs []string
53+
func runRm(ctx context.Context, dockerCLI command.Cli, opts *rmOptions) error {
54+
apiClient := dockerCLI.Client()
5555
errChan := parallelOperation(ctx, opts.containers, func(ctx context.Context, ctrID string) error {
5656
ctrID = strings.Trim(ctrID, "/")
5757
if ctrID == "" {
58-
return errors.New("Container name cannot be empty")
58+
return errors.New("container name cannot be empty")
5959
}
60-
return dockerCli.Client().ContainerRemove(ctx, ctrID, container.RemoveOptions{
60+
return apiClient.ContainerRemove(ctx, ctrID, container.RemoveOptions{
6161
RemoveVolumes: opts.rmVolumes,
6262
RemoveLinks: opts.rmLink,
6363
Force: opts.force,
6464
})
6565
})
6666

67+
var errs []error
6768
for _, name := range opts.containers {
6869
if err := <-errChan; err != nil {
6970
if opts.force && errdefs.IsNotFound(err) {
70-
fmt.Fprintln(dockerCli.Err(), err)
71+
_, _ = fmt.Fprintln(dockerCLI.Err(), err)
7172
continue
7273
}
73-
errs = append(errs, err.Error())
74+
errs = append(errs, err)
7475
continue
7576
}
76-
fmt.Fprintln(dockerCli.Out(), name)
77+
_, _ = fmt.Fprintln(dockerCLI.Out(), name)
7778
}
78-
if len(errs) > 0 {
79-
return errors.New(strings.Join(errs, "\n"))
80-
}
81-
return nil
79+
return errors.Join(errs...)
8280
}

cli/command/container/stats.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package container
33
import (
44
"bytes"
55
"context"
6+
"errors"
67
"fmt"
78
"io"
89
"strings"
@@ -17,7 +18,6 @@ import (
1718
"github.com/docker/docker/api/types/container"
1819
"github.com/docker/docker/api/types/events"
1920
"github.com/docker/docker/api/types/filters"
20-
"github.com/pkg/errors"
2121
"github.com/sirupsen/logrus"
2222
"github.com/spf13/cobra"
2323
)
@@ -238,16 +238,16 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
238238
// make sure each container get at least one valid stat data
239239
waitFirst.Wait()
240240

241-
var errs []string
241+
var errs []error
242242
cStats.mu.RLock()
243243
for _, c := range cStats.cs {
244244
if err := c.GetError(); err != nil {
245-
errs = append(errs, err.Error())
245+
errs = append(errs, err)
246246
}
247247
}
248248
cStats.mu.RUnlock()
249-
if len(errs) > 0 {
250-
return errors.New(strings.Join(errs, "\n"))
249+
if err := errors.Join(errs...); err != nil {
250+
return err
251251
}
252252
}
253253

cli/command/container/stop.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package container
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
6-
"strings"
77

88
"github.com/docker/cli/cli"
99
"github.com/docker/cli/cli/command"
1010
"github.com/docker/cli/cli/command/completion"
1111
"github.com/docker/docker/api/types/container"
12-
"github.com/pkg/errors"
1312
"github.com/spf13/cobra"
1413
)
1514

@@ -56,28 +55,26 @@ func NewStopCommand(dockerCli command.Cli) *cobra.Command {
5655
return cmd
5756
}
5857

59-
func runStop(ctx context.Context, dockerCli command.Cli, opts *stopOptions) error {
58+
func runStop(ctx context.Context, dockerCLI command.Cli, opts *stopOptions) error {
6059
var timeout *int
6160
if opts.timeoutChanged {
6261
timeout = &opts.timeout
6362
}
6463

64+
apiClient := dockerCLI.Client()
6565
errChan := parallelOperation(ctx, opts.containers, func(ctx context.Context, id string) error {
66-
return dockerCli.Client().ContainerStop(ctx, id, container.StopOptions{
66+
return apiClient.ContainerStop(ctx, id, container.StopOptions{
6767
Signal: opts.signal,
6868
Timeout: timeout,
6969
})
7070
})
71-
var errs []string
71+
var errs []error
7272
for _, ctr := range opts.containers {
7373
if err := <-errChan; err != nil {
74-
errs = append(errs, err.Error())
74+
errs = append(errs, err)
7575
continue
7676
}
77-
_, _ = fmt.Fprintln(dockerCli.Out(), ctr)
77+
_, _ = fmt.Fprintln(dockerCLI.Out(), ctr)
7878
}
79-
if len(errs) > 0 {
80-
return errors.New(strings.Join(errs, "\n"))
81-
}
82-
return nil
79+
return errors.Join(errs...)
8380
}

cli/command/container/unpause.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package container
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
6-
"strings"
77

88
"github.com/docker/cli/cli"
99
"github.com/docker/cli/cli/command"
1010
"github.com/docker/cli/cli/command/completion"
1111
"github.com/docker/docker/api/types/container"
12-
"github.com/pkg/errors"
1312
"github.com/spf13/cobra"
1413
)
1514

@@ -39,18 +38,16 @@ func NewUnpauseCommand(dockerCli command.Cli) *cobra.Command {
3938
return cmd
4039
}
4140

42-
func runUnpause(ctx context.Context, dockerCli command.Cli, opts *unpauseOptions) error {
43-
var errs []string
44-
errChan := parallelOperation(ctx, opts.containers, dockerCli.Client().ContainerUnpause)
41+
func runUnpause(ctx context.Context, dockerCLI command.Cli, opts *unpauseOptions) error {
42+
apiClient := dockerCLI.Client()
43+
errChan := parallelOperation(ctx, opts.containers, apiClient.ContainerUnpause)
44+
var errs []error
4545
for _, ctr := range opts.containers {
4646
if err := <-errChan; err != nil {
47-
errs = append(errs, err.Error())
47+
errs = append(errs, err)
4848
continue
4949
}
50-
_, _ = fmt.Fprintln(dockerCli.Out(), ctr)
50+
_, _ = fmt.Fprintln(dockerCLI.Out(), ctr)
5151
}
52-
if len(errs) > 0 {
53-
return errors.New(strings.Join(errs, "\n"))
54-
}
55-
return nil
52+
return errors.Join(errs...)
5653
}

cli/command/container/wait.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ package container
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
6-
"strings"
77

88
"github.com/docker/cli/cli"
99
"github.com/docker/cli/cli/command"
1010
"github.com/docker/cli/cli/command/completion"
11-
"github.com/pkg/errors"
1211
"github.com/spf13/cobra"
1312
)
1413

@@ -37,20 +36,19 @@ func NewWaitCommand(dockerCli command.Cli) *cobra.Command {
3736
return cmd
3837
}
3938

40-
func runWait(ctx context.Context, dockerCli command.Cli, opts *waitOptions) error {
41-
var errs []string
39+
func runWait(ctx context.Context, dockerCLI command.Cli, opts *waitOptions) error {
40+
apiClient := dockerCLI.Client()
41+
42+
var errs []error
4243
for _, ctr := range opts.containers {
43-
resultC, errC := dockerCli.Client().ContainerWait(ctx, ctr, "")
44+
resultC, errC := apiClient.ContainerWait(ctx, ctr, "")
4445

4546
select {
4647
case result := <-resultC:
47-
_, _ = fmt.Fprintf(dockerCli.Out(), "%d\n", result.StatusCode)
48+
_, _ = fmt.Fprintf(dockerCLI.Out(), "%d\n", result.StatusCode)
4849
case err := <-errC:
49-
errs = append(errs, err.Error())
50+
errs = append(errs, err)
5051
}
5152
}
52-
if len(errs) > 0 {
53-
return errors.New(strings.Join(errs, "\n"))
54-
}
55-
return nil
53+
return errors.Join(errs...)
5654
}

0 commit comments

Comments
 (0)