remove.go 1.5 KB

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