2016-04-19 12:59:48 -04:00
|
|
|
package volume
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
2016-04-19 12:59:48 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2016-06-10 10:40:09 -04:00
|
|
|
type removeOptions struct {
|
|
|
|
force bool
|
|
|
|
|
|
|
|
volumes []string
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func newRemoveCommand(dockerCli *command.DockerCli) *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")
|
|
|
|
|
|
|
|
return cmd
|
2016-04-19 12:59:48 -04:00
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func runRemove(dockerCli *command.DockerCli, 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()
|
|
|
|
status := 0
|
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 {
|
2016-04-19 12:59:48 -04:00
|
|
|
fmt.Fprintf(dockerCli.Err(), "%s\n", err)
|
|
|
|
status = 1
|
|
|
|
continue
|
|
|
|
}
|
2016-07-04 00:02:56 -04:00
|
|
|
fmt.Fprintf(dockerCli.Out(), "%s\n", name)
|
2016-04-19 12:59:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if status != 0 {
|
|
|
|
return cli.StatusError{StatusCode: status}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
`
|