github.com/tsuna/docker@v1.7.0-rc3/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 flag "github.com/docker/docker/pkg/mflag" 10 ) 11 12 // CmdRmi removes all images with the specified name(s). 13 // 14 // Usage: docker rmi [OPTIONS] IMAGE [IMAGE...] 15 func (cli *DockerCli) CmdRmi(args ...string) error { 16 var ( 17 cmd = cli.Subcmd("rmi", "IMAGE [IMAGE...]", "Remove one or more images", 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 ) 21 cmd.Require(flag.Min, 1) 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 rdr, _, 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 dels := []types.ImageDelete{} 40 if err := json.NewDecoder(rdr).Decode(&dels); err != nil { 41 fmt.Fprintf(cli.err, "%s\n", err) 42 errNames = append(errNames, name) 43 continue 44 } 45 46 for _, del := range dels { 47 if del.Deleted != "" { 48 fmt.Fprintf(cli.out, "Deleted: %s\n", del.Deleted) 49 } else { 50 fmt.Fprintf(cli.out, "Untagged: %s\n", del.Untagged) 51 } 52 } 53 } 54 } 55 if len(errNames) > 0 { 56 return fmt.Errorf("Error: failed to remove images: %v", errNames) 57 } 58 return nil 59 }