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