remove.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package secret
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/docker/docker/cli"
  6. "github.com/docker/docker/cli/command"
  7. "github.com/spf13/cobra"
  8. )
  9. type removeOptions struct {
  10. ids []string
  11. }
  12. func newSecretRemoveCommand(dockerCli *command.DockerCli) *cobra.Command {
  13. return &cobra.Command{
  14. Use: "rm [id]",
  15. Short: "Remove a secret",
  16. Args: cli.RequiresMinArgs(1),
  17. RunE: func(cmd *cobra.Command, args []string) error {
  18. opts := removeOptions{
  19. ids: args,
  20. }
  21. return runSecretRemove(dockerCli, opts)
  22. },
  23. }
  24. }
  25. func runSecretRemove(dockerCli *command.DockerCli, opts removeOptions) error {
  26. client := dockerCli.Client()
  27. ctx := context.Background()
  28. // attempt to lookup secret by name
  29. secrets, err := getSecrets(client, ctx, opts.ids)
  30. if err != nil {
  31. return err
  32. }
  33. ids := opts.ids
  34. names := make(map[string]int)
  35. for _, id := range ids {
  36. names[id] = 1
  37. }
  38. if len(secrets) > 0 {
  39. ids = []string{}
  40. for _, s := range secrets {
  41. if _, ok := names[s.Spec.Annotations.Name]; ok {
  42. ids = append(ids, s.ID)
  43. }
  44. }
  45. }
  46. for _, id := range ids {
  47. if err := client.SecretRemove(ctx, id); err != nil {
  48. return err
  49. }
  50. fmt.Fprintln(dockerCli.Out(), id)
  51. }
  52. return nil
  53. }