remove.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package volume
  2. import (
  3. "fmt"
  4. "golang.org/x/net/context"
  5. "github.com/docker/docker/api/client"
  6. "github.com/docker/docker/cli"
  7. "github.com/spf13/cobra"
  8. )
  9. func newRemoveCommand(dockerCli *client.DockerCli) *cobra.Command {
  10. return &cobra.Command{
  11. Use: "rm VOLUME [VOLUME...]",
  12. Aliases: []string{"remove"},
  13. Short: "Remove one or more volumes",
  14. Long: removeDescription,
  15. Example: removeExample,
  16. Args: cli.RequiresMinArgs(1),
  17. RunE: func(cmd *cobra.Command, args []string) error {
  18. return runRemove(dockerCli, args)
  19. },
  20. }
  21. }
  22. func runRemove(dockerCli *client.DockerCli, volumes []string) error {
  23. client := dockerCli.Client()
  24. ctx := context.Background()
  25. status := 0
  26. for _, name := range volumes {
  27. if err := client.VolumeRemove(ctx, name); err != nil {
  28. fmt.Fprintf(dockerCli.Err(), "%s\n", err)
  29. status = 1
  30. continue
  31. }
  32. fmt.Fprintf(dockerCli.Out(), "%s\n", name)
  33. }
  34. if status != 0 {
  35. return cli.StatusError{StatusCode: status}
  36. }
  37. return nil
  38. }
  39. var removeDescription = `
  40. Remove one or more volumes. You cannot remove a volume that is in use by a container.
  41. `
  42. var removeExample = `
  43. $ docker volume rm hello
  44. hello
  45. `