cli/command/secret: use errors.Join

- Use stdlib multi-errors instead of creating our own
- use apiClient instead of client for the API client to
  prevent shadowing imports.
- use dockerCLI with Go's standard camelCase casing.
- rename runSecretRemove to runRemove to align with other commands

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2025-02-03 19:33:58 +01:00
parent 2a9fd4a939
commit f9e4335564
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C

View File

@ -2,12 +2,11 @@ package secret
import (
"context"
"errors"
"fmt"
"strings"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
@ -15,7 +14,7 @@ type removeOptions struct {
names []string
}
func newSecretRemoveCommand(dockerCli command.Cli) *cobra.Command {
func newSecretRemoveCommand(dockerCLI command.Cli) *cobra.Command {
return &cobra.Command{
Use: "rm SECRET [SECRET...]",
Aliases: []string{"remove"},
@ -25,31 +24,24 @@ func newSecretRemoveCommand(dockerCli command.Cli) *cobra.Command {
opts := removeOptions{
names: args,
}
return runSecretRemove(cmd.Context(), dockerCli, opts)
return runRemove(cmd.Context(), dockerCLI, opts)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completeNames(dockerCli)(cmd, args, toComplete)
return completeNames(dockerCLI)(cmd, args, toComplete)
},
}
}
func runSecretRemove(ctx context.Context, dockerCli command.Cli, opts removeOptions) error {
client := dockerCli.Client()
var errs []string
func runRemove(ctx context.Context, dockerCLI command.Cli, opts removeOptions) error {
apiClient := dockerCLI.Client()
var errs []error
for _, name := range opts.names {
if err := client.SecretRemove(ctx, name); err != nil {
errs = append(errs, err.Error())
if err := apiClient.SecretRemove(ctx, name); err != nil {
errs = append(errs, err)
continue
}
fmt.Fprintln(dockerCli.Out(), name)
_, _ = fmt.Fprintln(dockerCLI.Out(), name)
}
if len(errs) > 0 {
return errors.Errorf("%s", strings.Join(errs, "\n"))
}
return nil
return errors.Join(errs...)
}