github.com/squaremo/docker@v1.3.2-0.20150516120342-42cfc9554972/api/client/rm.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"strings"
     7  
     8  	flag "github.com/docker/docker/pkg/mflag"
     9  )
    10  
    11  // CmdRm removes one or more containers.
    12  //
    13  // Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
    14  func (cli *DockerCli) CmdRm(args ...string) error {
    15  	cmd := cli.Subcmd("rm", "CONTAINER [CONTAINER...]", "Remove one or more containers", true)
    16  	v := cmd.Bool([]string{"v", "-volumes"}, false, "Remove the volumes associated with the container")
    17  	link := cmd.Bool([]string{"l", "#link", "-link"}, false, "Remove the specified link")
    18  	force := cmd.Bool([]string{"f", "-force"}, false, "Force the removal of a running container (uses SIGKILL)")
    19  	cmd.Require(flag.Min, 1)
    20  
    21  	cmd.ParseFlags(args, true)
    22  
    23  	val := url.Values{}
    24  	if *v {
    25  		val.Set("v", "1")
    26  	}
    27  	if *link {
    28  		val.Set("link", "1")
    29  	}
    30  
    31  	if *force {
    32  		val.Set("force", "1")
    33  	}
    34  
    35  	var errNames []string
    36  	for _, name := range cmd.Args() {
    37  		if name == "" {
    38  			return fmt.Errorf("Container name cannot be empty")
    39  		}
    40  		name = strings.Trim(name, "/")
    41  
    42  		_, _, err := readBody(cli.call("DELETE", "/containers/"+name+"?"+val.Encode(), nil, nil))
    43  		if err != nil {
    44  			fmt.Fprintf(cli.err, "%s\n", err)
    45  			errNames = append(errNames, name)
    46  		} else {
    47  			fmt.Fprintf(cli.out, "%s\n", name)
    48  		}
    49  	}
    50  	if len(errNames) > 0 {
    51  		return fmt.Errorf("Error: failed to remove containers: %v", errNames)
    52  	}
    53  	return nil
    54  }