remove.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package secret
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/docker/docker/cli"
  6. "github.com/docker/docker/cli/command"
  7. "github.com/spf13/cobra"
  8. "golang.org/x/net/context"
  9. )
  10. type removeOptions struct {
  11. names []string
  12. }
  13. func newSecretRemoveCommand(dockerCli *command.DockerCli) *cobra.Command {
  14. return &cobra.Command{
  15. Use: "rm SECRET [SECRET...]",
  16. Aliases: []string{"remove"},
  17. Short: "Remove one or more secrets",
  18. Args: cli.RequiresMinArgs(1),
  19. RunE: func(cmd *cobra.Command, args []string) error {
  20. opts := removeOptions{
  21. names: args,
  22. }
  23. return runSecretRemove(dockerCli, opts)
  24. },
  25. }
  26. }
  27. func runSecretRemove(dockerCli *command.DockerCli, opts removeOptions) error {
  28. client := dockerCli.Client()
  29. ctx := context.Background()
  30. ids, err := getCliRequestedSecretIDs(ctx, client, opts.names)
  31. if err != nil {
  32. return err
  33. }
  34. var errs []string
  35. for _, id := range ids {
  36. if err := client.SecretRemove(ctx, id); err != nil {
  37. errs = append(errs, err.Error())
  38. continue
  39. }
  40. fmt.Fprintln(dockerCli.Out(), id)
  41. }
  42. if len(errs) > 0 {
  43. return fmt.Errorf("%s", strings.Join(errs, "\n"))
  44. }
  45. return nil
  46. }