github.com/a4a881d4/docker@v1.9.0-rc2/api/client/rmi.go (about)

     1  package client
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/url"
     7  
     8  	"github.com/docker/docker/api/types"
     9  	Cli "github.com/docker/docker/cli"
    10  	flag "github.com/docker/docker/pkg/mflag"
    11  )
    12  
    13  // CmdRmi removes all images with the specified name(s).
    14  //
    15  // Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]
    16  func (cli *DockerCli) CmdRmi(args ...string) error {
    17  	cmd := Cli.Subcmd("rmi", []string{"IMAGE [IMAGE...]"}, Cli.DockerCommands["rmi"].Description, true)
    18  	force := cmd.Bool([]string{"f", "-force"}, false, "Force removal of the image")
    19  	noprune := cmd.Bool([]string{"-no-prune"}, false, "Do not delete untagged parents")
    20  	cmd.Require(flag.Min, 1)
    21  
    22  	cmd.ParseFlags(args, true)
    23  
    24  	v := url.Values{}
    25  	if *force {
    26  		v.Set("force", "1")
    27  	}
    28  	if *noprune {
    29  		v.Set("noprune", "1")
    30  	}
    31  
    32  	var errNames []string
    33  	for _, name := range cmd.Args() {
    34  		serverResp, err := cli.call("DELETE", "/images/"+name+"?"+v.Encode(), nil, nil)
    35  		if err != nil {
    36  			fmt.Fprintf(cli.err, "%s\n", err)
    37  			errNames = append(errNames, name)
    38  		} else {
    39  			defer serverResp.body.Close()
    40  
    41  			dels := []types.ImageDelete{}
    42  			if err := json.NewDecoder(serverResp.body).Decode(&dels); err != nil {
    43  				fmt.Fprintf(cli.err, "%s\n", err)
    44  				errNames = append(errNames, name)
    45  				continue
    46  			}
    47  
    48  			for _, del := range dels {
    49  				if del.Deleted != "" {
    50  					fmt.Fprintf(cli.out, "Deleted: %s\n", del.Deleted)
    51  				} else {
    52  					fmt.Fprintf(cli.out, "Untagged: %s\n", del.Untagged)
    53  				}
    54  			}
    55  		}
    56  	}
    57  	if len(errNames) > 0 {
    58  		return fmt.Errorf("Error: failed to remove images: %v", errNames)
    59  	}
    60  	return nil
    61  }