remove.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. Short: "Remove one or more secrets",
  17. Args: cli.RequiresMinArgs(1),
  18. RunE: func(cmd *cobra.Command, args []string) error {
  19. opts := removeOptions{
  20. names: args,
  21. }
  22. return runSecretRemove(dockerCli, opts)
  23. },
  24. }
  25. }
  26. func runSecretRemove(dockerCli *command.DockerCli, opts removeOptions) error {
  27. client := dockerCli.Client()
  28. ctx := context.Background()
  29. ids, err := getCliRequestedSecretIDs(ctx, client, opts.names)
  30. if err != nil {
  31. return err
  32. }
  33. var errs []string
  34. for _, id := range ids {
  35. if err := client.SecretRemove(ctx, id); err != nil {
  36. errs = append(errs, err.Error())
  37. continue
  38. }
  39. fmt.Fprintln(dockerCli.Out(), id)
  40. }
  41. if len(errs) > 0 {
  42. return fmt.Errorf("%s", strings.Join(errs, "\n"))
  43. }
  44. return nil
  45. }