2016-06-06 11:32:38 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-06-06 11:32:38 -04:00
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
2017-03-27 21:21:59 -04:00
|
|
|
"github.com/pkg/errors"
|
2016-06-06 11:32:38 -04:00
|
|
|
"github.com/spf13/cobra"
|
2016-12-24 19:05:37 -05:00
|
|
|
"golang.org/x/net/context"
|
2016-06-06 11:32:38 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type rmOptions struct {
|
|
|
|
rmVolumes bool
|
|
|
|
rmLink bool
|
|
|
|
force bool
|
|
|
|
|
|
|
|
containers []string
|
|
|
|
}
|
|
|
|
|
2016-06-21 03:28:30 -04:00
|
|
|
// NewRmCommand creates a new cobra.Command for `docker rm`
|
2016-09-08 13:11:39 -04:00
|
|
|
func NewRmCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-06-06 11:32:38 -04:00
|
|
|
var opts rmOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "rm [OPTIONS] CONTAINER [CONTAINER...]",
|
|
|
|
Short: "Remove one or more containers",
|
|
|
|
Args: cli.RequiresMinArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
opts.containers = args
|
|
|
|
return runRm(dockerCli, &opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.BoolVarP(&opts.rmVolumes, "volumes", "v", false, "Remove the volumes associated with the container")
|
|
|
|
flags.BoolVarP(&opts.rmLink, "link", "l", false, "Remove the specified link")
|
|
|
|
flags.BoolVarP(&opts.force, "force", "f", false, "Force the removal of a running container (uses SIGKILL)")
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func runRm(dockerCli *command.DockerCli, opts *rmOptions) error {
|
2016-06-06 11:32:38 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
var errs []string
|
2016-09-21 10:35:08 -04:00
|
|
|
options := types.ContainerRemoveOptions{
|
|
|
|
RemoveVolumes: opts.rmVolumes,
|
|
|
|
RemoveLinks: opts.rmLink,
|
|
|
|
Force: opts.force,
|
|
|
|
}
|
|
|
|
|
|
|
|
errChan := parallelOperation(ctx, opts.containers, func(ctx context.Context, container string) error {
|
2016-12-24 19:05:37 -05:00
|
|
|
container = strings.Trim(container, "/")
|
2016-09-21 10:35:08 -04:00
|
|
|
if container == "" {
|
2016-12-24 19:05:37 -05:00
|
|
|
return errors.New("Container name cannot be empty")
|
2016-06-06 11:32:38 -04:00
|
|
|
}
|
2016-09-21 10:35:08 -04:00
|
|
|
return dockerCli.Client().ContainerRemove(ctx, container, options)
|
|
|
|
})
|
2016-06-06 11:32:38 -04:00
|
|
|
|
2016-09-21 10:35:08 -04:00
|
|
|
for _, name := range opts.containers {
|
|
|
|
if err := <-errChan; err != nil {
|
2016-06-06 11:32:38 -04:00
|
|
|
errs = append(errs, err.Error())
|
2016-12-24 19:05:37 -05:00
|
|
|
continue
|
2016-06-06 11:32:38 -04:00
|
|
|
}
|
2016-12-24 19:05:37 -05:00
|
|
|
fmt.Fprintln(dockerCli.Out(), name)
|
2016-06-06 11:32:38 -04:00
|
|
|
}
|
|
|
|
if len(errs) > 0 {
|
2016-12-24 19:05:37 -05:00
|
|
|
return errors.New(strings.Join(errs, "\n"))
|
2016-06-06 11:32:38 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|