2016-04-19 12:59:48 -04:00
|
|
|
package volume
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-02-27 12:39:35 -05:00
|
|
|
"strings"
|
2016-04-19 12:59:48 -04:00
|
|
|
|
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-04-19 12:59:48 -04:00
|
|
|
"github.com/spf13/cobra"
|
2017-02-27 12:39:35 -05:00
|
|
|
"golang.org/x/net/context"
|
2016-04-19 12:59:48 -04:00
|
|
|
)
|
|
|
|
|
2016-06-10 10:40:09 -04:00
|
|
|
type removeOptions struct {
|
|
|
|
force bool
|
|
|
|
|
|
|
|
volumes []string
|
|
|
|
}
|
|
|
|
|
2017-02-27 12:39:35 -05:00
|
|
|
func newRemoveCommand(dockerCli command.Cli) *cobra.Command {
|
2016-06-10 10:40:09 -04:00
|
|
|
var opts removeOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2016-08-30 11:07:42 -04:00
|
|
|
Use: "rm [OPTIONS] VOLUME [VOLUME...]",
|
2016-04-19 12:59:48 -04:00
|
|
|
Aliases: []string{"remove"},
|
2016-07-29 13:41:52 -04:00
|
|
|
Short: "Remove one or more volumes",
|
2016-06-20 16:33:53 -04:00
|
|
|
Long: removeDescription,
|
|
|
|
Example: removeExample,
|
2016-05-26 17:57:31 -04:00
|
|
|
Args: cli.RequiresMinArgs(1),
|
2016-04-19 12:59:48 -04:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2016-06-10 10:40:09 -04:00
|
|
|
opts.volumes = args
|
|
|
|
return runRemove(dockerCli, &opts)
|
2016-04-19 12:59:48 -04:00
|
|
|
},
|
|
|
|
}
|
2016-06-10 10:40:09 -04:00
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.BoolVarP(&opts.force, "force", "f", false, "Force the removal of one or more volumes")
|
2016-11-02 20:43:32 -04:00
|
|
|
flags.SetAnnotation("force", "version", []string{"1.25"})
|
2016-06-10 10:40:09 -04:00
|
|
|
return cmd
|
2016-04-19 12:59:48 -04:00
|
|
|
}
|
|
|
|
|
2017-02-27 12:39:35 -05:00
|
|
|
func runRemove(dockerCli command.Cli, opts *removeOptions) error {
|
2016-04-19 12:59:48 -04:00
|
|
|
client := dockerCli.Client()
|
2016-05-26 17:57:31 -04:00
|
|
|
ctx := context.Background()
|
2017-02-27 12:39:35 -05:00
|
|
|
|
|
|
|
var errs []string
|
2016-04-19 12:59:48 -04:00
|
|
|
|
2016-06-10 10:40:09 -04:00
|
|
|
for _, name := range opts.volumes {
|
|
|
|
if err := client.VolumeRemove(ctx, name, opts.force); err != nil {
|
2017-02-27 12:39:35 -05:00
|
|
|
errs = append(errs, err.Error())
|
2016-04-19 12:59:48 -04:00
|
|
|
continue
|
|
|
|
}
|
2016-07-04 00:02:56 -04:00
|
|
|
fmt.Fprintf(dockerCli.Out(), "%s\n", name)
|
2016-04-19 12:59:48 -04:00
|
|
|
}
|
|
|
|
|
2017-02-27 12:39:35 -05:00
|
|
|
if len(errs) > 0 {
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf("%s", strings.Join(errs, "\n"))
|
2016-04-19 12:59:48 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-06-20 16:33:53 -04:00
|
|
|
|
|
|
|
var removeDescription = `
|
2016-07-29 13:41:52 -04:00
|
|
|
Remove one or more volumes. You cannot remove a volume that is in use by a container.
|
2016-06-20 16:33:53 -04:00
|
|
|
`
|
|
|
|
|
|
|
|
var removeExample = `
|
|
|
|
$ docker volume rm hello
|
|
|
|
hello
|
|
|
|
`
|