remove.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. var errs []string
  31. for _, name := range opts.names {
  32. if err := client.SecretRemove(ctx, name); err != nil {
  33. errs = append(errs, err.Error())
  34. continue
  35. }
  36. fmt.Fprintln(dockerCli.Out(), name)
  37. }
  38. if len(errs) > 0 {
  39. return fmt.Errorf("%s", strings.Join(errs, "\n"))
  40. }
  41. return nil
  42. }