From 71ebbb81ae3c8509131985c60f5b0dd9c9e93d5a Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 19 Oct 2024 10:25:50 +0200 Subject: [PATCH 1/3] cli/command/plugins: use errors.Join instead of custom cli.Errors This command was using a custom "multi-error" implementation, but it had some limitations, and the formatting wasn't great. This patch replaces it with Go's errors.Join. Before: docker plugin remove one two three Error response from daemon: plugin "one" not found, Error response from daemon: plugin "two" not found, Error response from daemon: plugin "three" not found After: docker plugin remove one two three Error response from daemon: plugin "one" not found Error response from daemon: plugin "two" not found Error response from daemon: plugin "three" not found Signed-off-by: Sebastiaan van Stijn --- cli/command/plugin/remove.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/cli/command/plugin/remove.go b/cli/command/plugin/remove.go index 2614cab3ac..36b0013105 100644 --- a/cli/command/plugin/remove.go +++ b/cli/command/plugin/remove.go @@ -2,6 +2,7 @@ package plugin import ( "context" + "errors" "fmt" "github.com/docker/cli/cli" @@ -36,17 +37,13 @@ func newRemoveCommand(dockerCli command.Cli) *cobra.Command { } func runRemove(ctx context.Context, dockerCli command.Cli, opts *rmOptions) error { - var errs cli.Errors + var errs error for _, name := range opts.plugins { if err := dockerCli.Client().PluginRemove(ctx, name, types.PluginRemoveOptions{Force: opts.force}); err != nil { - errs = append(errs, err) + errs = errors.Join(errs, err) continue } - fmt.Fprintln(dockerCli.Out(), name) + _, _ = fmt.Fprintln(dockerCli.Out(), name) } - // Do not simplify to `return errs` because even if errs == nil, it is not a nil-error interface value. - if errs != nil { - return errs - } - return nil + return errs } From d3bafa5f3ee9089f71d4c3e3d08267521107d73b Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 19 Oct 2024 10:31:08 +0200 Subject: [PATCH 2/3] cli: deprecate Errors type The Errors type is no longer used by the CLI itself, and this custom "multi-error" implementation had both limitations (empty list not being `nil`), as well as formatting not being great. All of this making it not something to recommend, and better handled with Go's stdlib. As far as I could find, there's no external consumers of this, but let's deprecate first, and remove in the next release. Signed-off-by: Sebastiaan van Stijn --- cli/error.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cli/error.go b/cli/error.go index 8fb2ab3671..b1198997b4 100644 --- a/cli/error.go +++ b/cli/error.go @@ -8,6 +8,8 @@ import ( // Errors is a list of errors. // Useful in a loop if you don't want to return the error right away and you want to display after the loop, // all the errors that happened during the loop. +// +// Deprecated: use [errors.Join] instead; will be removed in the next release. type Errors []error func (errList Errors) Error() string { From 6460abcf1a20e33b2cbedd2f348c7b0dff4d84d6 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 19 Oct 2024 11:00:38 +0200 Subject: [PATCH 3/3] cli: remove deprecated Errors type The Errors type was deprecated in d3bafa5f3ee9089f71d4c3e3d08267521107d73b, which has been included in the 27.4.0 release. This patch removes the type, as there are no external consumers. Signed-off-by: Sebastiaan van Stijn --- cli/error.go | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/cli/error.go b/cli/error.go index b1198997b4..8c4a5f952b 100644 --- a/cli/error.go +++ b/cli/error.go @@ -2,28 +2,8 @@ package cli import ( "strconv" - "strings" ) -// Errors is a list of errors. -// Useful in a loop if you don't want to return the error right away and you want to display after the loop, -// all the errors that happened during the loop. -// -// Deprecated: use [errors.Join] instead; will be removed in the next release. -type Errors []error - -func (errList Errors) Error() string { - if len(errList) < 1 { - return "" - } - - out := make([]string, len(errList)) - for i := range errList { - out[i] = errList[i].Error() - } - return strings.Join(out, ", ") -} - // StatusError reports an unsuccessful exit by a command. type StatusError struct { Status string