github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podmanV2/images/rm.go (about) 1 package images 2 3 import ( 4 "fmt" 5 "os" 6 7 "github.com/containers/libpod/cmd/podmanV2/registry" 8 "github.com/containers/libpod/pkg/domain/entities" 9 "github.com/pkg/errors" 10 "github.com/spf13/cobra" 11 "github.com/spf13/pflag" 12 ) 13 14 var ( 15 rmDescription = "Removes one or more previously pulled or locally created images." 16 rmCmd = &cobra.Command{ 17 Use: "rm [flags] IMAGE [IMAGE...]", 18 Short: "Removes one or more images from local storage", 19 Long: rmDescription, 20 PreRunE: preRunE, 21 RunE: rm, 22 Example: `podman image rm imageID 23 podman image rm --force alpine 24 podman image rm c4dfb1609ee2 93fd78260bd1 c0ed59d05ff7`, 25 } 26 27 imageOpts = entities.ImageDeleteOptions{} 28 ) 29 30 func init() { 31 registry.Commands = append(registry.Commands, registry.CliCommand{ 32 Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode}, 33 Command: rmCmd, 34 Parent: imageCmd, 35 }) 36 37 imageRemoveFlagSet(rmCmd.Flags()) 38 } 39 40 func imageRemoveFlagSet(flags *pflag.FlagSet) { 41 flags.BoolVarP(&imageOpts.All, "all", "a", false, "Remove all images") 42 flags.BoolVarP(&imageOpts.Force, "force", "f", false, "Force Removal of the image") 43 } 44 func rm(cmd *cobra.Command, args []string) error { 45 46 if len(args) < 1 && !imageOpts.All { 47 return errors.Errorf("image name or ID must be specified") 48 } 49 if len(args) > 0 && imageOpts.All { 50 return errors.Errorf("when using the --all switch, you may not pass any images names or IDs") 51 } 52 report, err := registry.ImageEngine().Delete(registry.GetContext(), args, imageOpts) 53 if err != nil { 54 switch { 55 case report != nil && report.ImageNotFound != nil: 56 fmt.Fprintln(os.Stderr, err.Error()) 57 registry.SetExitCode(2) 58 case report != nil && report.ImageInUse != nil: 59 fmt.Fprintln(os.Stderr, err.Error()) 60 default: 61 return err 62 } 63 } 64 65 for _, u := range report.Untagged { 66 fmt.Println("Untagged: " + u) 67 } 68 for _, d := range report.Deleted { 69 fmt.Println("Deleted: " + d) 70 } 71 return nil 72 }