11package main
22
33import (
4- "crypto/sha256"
5- "fmt"
64 "os"
7- "path/filepath"
85
6+ "github.com/lima-vm/lima/pkg/downloader"
97 "github.com/lima-vm/lima/pkg/limayaml"
108 "github.com/lima-vm/lima/pkg/store"
119 "github.com/lima-vm/lima/pkg/templatestore"
@@ -31,40 +29,27 @@ func pruneAction(cmd *cobra.Command, _ []string) error {
3129 if err != nil {
3230 return err
3331 }
34- ucd , err := os .UserCacheDir ()
35- if err != nil {
36- return err
37- }
38- cacheDir := filepath .Join (ucd , "lima" )
39- logrus .Infof ("Pruning %q" , cacheDir )
32+ opt := downloader .WithCache ()
4033 if ! keepReferred {
41- return os . RemoveAll ( cacheDir )
34+ return downloader . RemoveAllCacheDir ( opt )
4235 }
4336
4437 // Prune downloads that are not used by any instances or templates
45- downloadDir := filepath .Join (cacheDir , "download" , "by-url-sha256" )
46- _ , err = os .Stat (downloadDir )
47- if err != nil {
48- if os .IsNotExist (err ) {
49- return nil
50- }
51- return err
52- }
53- cacheEntries , err := os .ReadDir (downloadDir )
38+ cacheEntries , err := downloader .CacheEntries (opt )
5439 if err != nil {
5540 return err
5641 }
5742 knownLocations , err := knownLocations ()
5843 if err != nil {
5944 return err
6045 }
61- for _ , entry := range cacheEntries {
62- if file , exists := knownLocations [entry . Name () ]; exists {
63- logrus .Debugf ("Keep %q caching %q" , entry . Name () , file .Location )
46+ for cacheKey , cachePath := range cacheEntries {
47+ if file , exists := knownLocations [cacheKey ]; exists {
48+ logrus .Debugf ("Keep %q caching %q" , cacheKey , file .Location )
6449 } else {
65- logrus .Debug ("Deleting " , entry . Name () )
66- if err := os .RemoveAll (filepath . Join ( downloadDir , entry . Name ()) ); err != nil {
67- logrus .Warnf ("Failed to delete %q: %v" , entry . Name () , err )
50+ logrus .Debug ("Deleting " , cacheKey )
51+ if err := os .RemoveAll (cachePath ); err != nil {
52+ logrus .Warnf ("Failed to delete %q: %v" , cacheKey , err )
6853 return err
6954 }
7055 }
@@ -114,23 +99,19 @@ func knownLocations() (map[string]limayaml.File, error) {
11499func locationsFromLimaYAML (y * limayaml.LimaYAML ) map [string ]limayaml.File {
115100 locations := make (map [string ]limayaml.File )
116101 for _ , f := range y .Images {
117- locations [sha256OfURL (f .Location )] = f .File
102+ locations [downloader . CacheKey (f .Location )] = f .File
118103 if f .Kernel != nil {
119- locations [sha256OfURL (f .Kernel .Location )] = f .Kernel .File
104+ locations [downloader . CacheKey (f .Kernel .Location )] = f .Kernel .File
120105 }
121106 if f .Initrd != nil {
122- locations [sha256OfURL (f .Initrd .Location )] = * f .Initrd
107+ locations [downloader . CacheKey (f .Initrd .Location )] = * f .Initrd
123108 }
124109 }
125110 for _ , f := range y .Containerd .Archives {
126- locations [sha256OfURL (f .Location )] = f
111+ locations [downloader . CacheKey (f .Location )] = f
127112 }
128113 for _ , f := range y .Firmware .Images {
129- locations [sha256OfURL (f .Location )] = f .File
114+ locations [downloader . CacheKey (f .Location )] = f .File
130115 }
131116 return locations
132117}
133-
134- func sha256OfURL (url string ) string {
135- return fmt .Sprintf ("%x" , sha256 .Sum256 ([]byte (url )))
136- }
0 commit comments