github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podmanV2/images/prune.go (about) 1 package images 2 3 import ( 4 "bufio" 5 "fmt" 6 "os" 7 "strings" 8 9 "github.com/containers/libpod/cmd/podmanV2/registry" 10 "github.com/containers/libpod/pkg/domain/entities" 11 "github.com/pkg/errors" 12 "github.com/spf13/cobra" 13 ) 14 15 var ( 16 pruneDescription = `Removes all unnamed images from local storage. 17 18 If an image is not being used by a container, it will be removed from the system.` 19 pruneCmd = &cobra.Command{ 20 Use: "prune", 21 Args: cobra.NoArgs, 22 Short: "Remove unused images", 23 Long: pruneDescription, 24 RunE: prune, 25 Example: `podman image prune`, 26 } 27 28 pruneOpts = entities.ImagePruneOptions{} 29 force bool 30 filter = []string{} 31 ) 32 33 func init() { 34 registry.Commands = append(registry.Commands, registry.CliCommand{ 35 Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode}, 36 Command: pruneCmd, 37 Parent: imageCmd, 38 }) 39 40 flags := pruneCmd.Flags() 41 flags.BoolVarP(&pruneOpts.All, "all", "a", false, "Remove all unused images, not just dangling ones") 42 flags.BoolVarP(&force, "force", "f", false, "Do not prompt for confirmation") 43 flags.StringArrayVar(&filter, "filter", []string{}, "Provide filter values (e.g. 'label=<key>=<value>')") 44 45 } 46 47 func prune(cmd *cobra.Command, args []string) error { 48 if !force { 49 reader := bufio.NewReader(os.Stdin) 50 fmt.Printf(` 51 WARNING! This will remove all dangling images. 52 Are you sure you want to continue? [y/N] `) 53 answer, err := reader.ReadString('\n') 54 if err != nil { 55 return errors.Wrapf(err, "error reading input") 56 } 57 if strings.ToLower(answer)[0] != 'y' { 58 return nil 59 } 60 } 61 62 // TODO Remove once filter refactor is finished and url.Values rules :) 63 for _, f := range filter { 64 t := strings.SplitN(f, "=", 2) 65 pruneOpts.Filters.Add(t[0], t[1]) 66 } 67 68 results, err := registry.ImageEngine().Prune(registry.GetContext(), pruneOpts) 69 if err != nil { 70 return err 71 } 72 73 for _, i := range results.Report.Id { 74 fmt.Println(i) 75 } 76 77 for _, e := range results.Report.Err { 78 fmt.Fprint(os.Stderr, e.Error()+"\n") 79 } 80 81 if results.Size > 0 { 82 fmt.Fprintf(os.Stdout, "Size: %d\n", results.Size) 83 } 84 85 return nil 86 }