diff --git a/cli/command/container/kill.go b/cli/command/container/kill.go index 0095198b5a..3d5c599418 100644 --- a/cli/command/container/kill.go +++ b/cli/command/container/kill.go @@ -2,13 +2,12 @@ package container import ( "context" + "errors" "fmt" - "strings" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command/completion" - "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -44,20 +43,19 @@ func NewKillCommand(dockerCli command.Cli) *cobra.Command { return cmd } -func runKill(ctx context.Context, dockerCli command.Cli, opts *killOptions) error { - var errs []string +func runKill(ctx context.Context, dockerCLI command.Cli, opts *killOptions) error { + apiClient := dockerCLI.Client() errChan := parallelOperation(ctx, opts.containers, func(ctx context.Context, container string) error { - return dockerCli.Client().ContainerKill(ctx, container, opts.signal) + return apiClient.ContainerKill(ctx, container, opts.signal) }) + + var errs []error for _, name := range opts.containers { if err := <-errChan; err != nil { - errs = append(errs, err.Error()) - } else { - _, _ = fmt.Fprintln(dockerCli.Out(), name) + errs = append(errs, err) + continue } + _, _ = fmt.Fprintln(dockerCLI.Out(), name) } - if len(errs) > 0 { - return errors.New(strings.Join(errs, "\n")) - } - return nil + return errors.Join(errs...) } diff --git a/cli/command/container/pause.go b/cli/command/container/pause.go index 87fb0e10c5..b2a40aa705 100644 --- a/cli/command/container/pause.go +++ b/cli/command/container/pause.go @@ -2,14 +2,13 @@ package container import ( "context" + "errors" "fmt" - "strings" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command/completion" "github.com/docker/docker/api/types/container" - "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -38,18 +37,17 @@ func NewPauseCommand(dockerCli command.Cli) *cobra.Command { } } -func runPause(ctx context.Context, dockerCli command.Cli, opts *pauseOptions) error { - var errs []string - errChan := parallelOperation(ctx, opts.containers, dockerCli.Client().ContainerPause) +func runPause(ctx context.Context, dockerCLI command.Cli, opts *pauseOptions) error { + apiClient := dockerCLI.Client() + errChan := parallelOperation(ctx, opts.containers, apiClient.ContainerPause) + + var errs []error for _, ctr := range opts.containers { if err := <-errChan; err != nil { - errs = append(errs, err.Error()) + errs = append(errs, err) continue } - _, _ = fmt.Fprintln(dockerCli.Out(), ctr) + _, _ = fmt.Fprintln(dockerCLI.Out(), ctr) } - if len(errs) > 0 { - return errors.New(strings.Join(errs, "\n")) - } - return nil + return errors.Join(errs...) } diff --git a/cli/command/container/restart.go b/cli/command/container/restart.go index 5460ff5228..379b6a12eb 100644 --- a/cli/command/container/restart.go +++ b/cli/command/container/restart.go @@ -2,14 +2,13 @@ package container import ( "context" + "errors" "fmt" - "strings" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command/completion" "github.com/docker/docker/api/types/container" - "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -56,27 +55,25 @@ func NewRestartCommand(dockerCli command.Cli) *cobra.Command { return cmd } -func runRestart(ctx context.Context, dockerCli command.Cli, opts *restartOptions) error { - var errs []string +func runRestart(ctx context.Context, dockerCLI command.Cli, opts *restartOptions) error { var timeout *int if opts.timeoutChanged { timeout = &opts.timeout } + apiClient := dockerCLI.Client() + var errs []error // TODO(thaJeztah): consider using parallelOperation for restart, similar to "stop" and "remove" for _, name := range opts.containers { - err := dockerCli.Client().ContainerRestart(ctx, name, container.StopOptions{ + err := apiClient.ContainerRestart(ctx, name, container.StopOptions{ Signal: opts.signal, Timeout: timeout, }) if err != nil { - errs = append(errs, err.Error()) + errs = append(errs, err) continue } - _, _ = fmt.Fprintln(dockerCli.Out(), name) + _, _ = fmt.Fprintln(dockerCLI.Out(), name) } - if len(errs) > 0 { - return errors.New(strings.Join(errs, "\n")) - } - return nil + return errors.Join(errs...) } diff --git a/cli/command/container/rm.go b/cli/command/container/rm.go index 12beef64f6..627b042f0b 100644 --- a/cli/command/container/rm.go +++ b/cli/command/container/rm.go @@ -2,6 +2,7 @@ package container import ( "context" + "errors" "fmt" "strings" @@ -10,7 +11,6 @@ import ( "github.com/docker/cli/cli/command/completion" "github.com/docker/docker/api/types/container" "github.com/docker/docker/errdefs" - "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -50,33 +50,31 @@ func NewRmCommand(dockerCli command.Cli) *cobra.Command { return cmd } -func runRm(ctx context.Context, dockerCli command.Cli, opts *rmOptions) error { - var errs []string +func runRm(ctx context.Context, dockerCLI command.Cli, opts *rmOptions) error { + apiClient := dockerCLI.Client() errChan := parallelOperation(ctx, opts.containers, func(ctx context.Context, ctrID string) error { ctrID = strings.Trim(ctrID, "/") if ctrID == "" { - return errors.New("Container name cannot be empty") + return errors.New("container name cannot be empty") } - return dockerCli.Client().ContainerRemove(ctx, ctrID, container.RemoveOptions{ + return apiClient.ContainerRemove(ctx, ctrID, container.RemoveOptions{ RemoveVolumes: opts.rmVolumes, RemoveLinks: opts.rmLink, Force: opts.force, }) }) + var errs []error for _, name := range opts.containers { if err := <-errChan; err != nil { if opts.force && errdefs.IsNotFound(err) { - fmt.Fprintln(dockerCli.Err(), err) + _, _ = fmt.Fprintln(dockerCLI.Err(), err) continue } - errs = append(errs, err.Error()) + errs = append(errs, err) continue } - fmt.Fprintln(dockerCli.Out(), name) + _, _ = fmt.Fprintln(dockerCLI.Out(), name) } - if len(errs) > 0 { - return errors.New(strings.Join(errs, "\n")) - } - return nil + return errors.Join(errs...) } diff --git a/cli/command/container/stats.go b/cli/command/container/stats.go index 76f7765cd6..5538e61dce 100644 --- a/cli/command/container/stats.go +++ b/cli/command/container/stats.go @@ -3,6 +3,7 @@ package container import ( "bytes" "context" + "errors" "fmt" "io" "strings" @@ -17,7 +18,6 @@ import ( "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/events" "github.com/docker/docker/api/types/filters" - "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -238,16 +238,16 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions) // make sure each container get at least one valid stat data waitFirst.Wait() - var errs []string + var errs []error cStats.mu.RLock() for _, c := range cStats.cs { if err := c.GetError(); err != nil { - errs = append(errs, err.Error()) + errs = append(errs, err) } } cStats.mu.RUnlock() - if len(errs) > 0 { - return errors.New(strings.Join(errs, "\n")) + if err := errors.Join(errs...); err != nil { + return err } } diff --git a/cli/command/container/stop.go b/cli/command/container/stop.go index af24f43caf..c6b331e964 100644 --- a/cli/command/container/stop.go +++ b/cli/command/container/stop.go @@ -2,14 +2,13 @@ package container import ( "context" + "errors" "fmt" - "strings" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command/completion" "github.com/docker/docker/api/types/container" - "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -56,28 +55,26 @@ func NewStopCommand(dockerCli command.Cli) *cobra.Command { return cmd } -func runStop(ctx context.Context, dockerCli command.Cli, opts *stopOptions) error { +func runStop(ctx context.Context, dockerCLI command.Cli, opts *stopOptions) error { var timeout *int if opts.timeoutChanged { timeout = &opts.timeout } + apiClient := dockerCLI.Client() errChan := parallelOperation(ctx, opts.containers, func(ctx context.Context, id string) error { - return dockerCli.Client().ContainerStop(ctx, id, container.StopOptions{ + return apiClient.ContainerStop(ctx, id, container.StopOptions{ Signal: opts.signal, Timeout: timeout, }) }) - var errs []string + var errs []error for _, ctr := range opts.containers { if err := <-errChan; err != nil { - errs = append(errs, err.Error()) + errs = append(errs, err) continue } - _, _ = fmt.Fprintln(dockerCli.Out(), ctr) + _, _ = fmt.Fprintln(dockerCLI.Out(), ctr) } - if len(errs) > 0 { - return errors.New(strings.Join(errs, "\n")) - } - return nil + return errors.Join(errs...) } diff --git a/cli/command/container/unpause.go b/cli/command/container/unpause.go index cffd94d4c2..7824f4a76f 100644 --- a/cli/command/container/unpause.go +++ b/cli/command/container/unpause.go @@ -2,14 +2,13 @@ package container import ( "context" + "errors" "fmt" - "strings" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command/completion" "github.com/docker/docker/api/types/container" - "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -39,18 +38,16 @@ func NewUnpauseCommand(dockerCli command.Cli) *cobra.Command { return cmd } -func runUnpause(ctx context.Context, dockerCli command.Cli, opts *unpauseOptions) error { - var errs []string - errChan := parallelOperation(ctx, opts.containers, dockerCli.Client().ContainerUnpause) +func runUnpause(ctx context.Context, dockerCLI command.Cli, opts *unpauseOptions) error { + apiClient := dockerCLI.Client() + errChan := parallelOperation(ctx, opts.containers, apiClient.ContainerUnpause) + var errs []error for _, ctr := range opts.containers { if err := <-errChan; err != nil { - errs = append(errs, err.Error()) + errs = append(errs, err) continue } - _, _ = fmt.Fprintln(dockerCli.Out(), ctr) + _, _ = fmt.Fprintln(dockerCLI.Out(), ctr) } - if len(errs) > 0 { - return errors.New(strings.Join(errs, "\n")) - } - return nil + return errors.Join(errs...) } diff --git a/cli/command/container/wait.go b/cli/command/container/wait.go index 8eb7d82f80..89f2ba17ac 100644 --- a/cli/command/container/wait.go +++ b/cli/command/container/wait.go @@ -2,13 +2,12 @@ package container import ( "context" + "errors" "fmt" - "strings" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command/completion" - "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -37,20 +36,19 @@ func NewWaitCommand(dockerCli command.Cli) *cobra.Command { return cmd } -func runWait(ctx context.Context, dockerCli command.Cli, opts *waitOptions) error { - var errs []string +func runWait(ctx context.Context, dockerCLI command.Cli, opts *waitOptions) error { + apiClient := dockerCLI.Client() + + var errs []error for _, ctr := range opts.containers { - resultC, errC := dockerCli.Client().ContainerWait(ctx, ctr, "") + resultC, errC := apiClient.ContainerWait(ctx, ctr, "") select { case result := <-resultC: - _, _ = fmt.Fprintf(dockerCli.Out(), "%d\n", result.StatusCode) + _, _ = fmt.Fprintf(dockerCLI.Out(), "%d\n", result.StatusCode) case err := <-errC: - errs = append(errs, err.Error()) + errs = append(errs, err) } } - if len(errs) > 0 { - return errors.New(strings.Join(errs, "\n")) - } - return nil + return errors.Join(errs...) }