1
1
package main
2
2
3
3
import (
4
- "crypto/sha256"
5
- "fmt"
6
4
"os"
7
- "path/filepath"
8
5
6
+ "github.com/lima-vm/lima/pkg/downloader"
9
7
"github.com/lima-vm/lima/pkg/limayaml"
10
8
"github.com/lima-vm/lima/pkg/store"
11
9
"github.com/lima-vm/lima/pkg/templatestore"
@@ -31,40 +29,27 @@ func pruneAction(cmd *cobra.Command, _ []string) error {
31
29
if err != nil {
32
30
return err
33
31
}
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 ()
40
33
if ! keepReferred {
41
- return os . RemoveAll ( cacheDir )
34
+ return downloader . RemoveAllCacheDir ( opt )
42
35
}
43
36
44
37
// 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 )
54
39
if err != nil {
55
40
return err
56
41
}
57
42
knownLocations , err := knownLocations ()
58
43
if err != nil {
59
44
return err
60
45
}
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 )
64
49
} 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 )
68
53
return err
69
54
}
70
55
}
@@ -114,23 +99,19 @@ func knownLocations() (map[string]limayaml.File, error) {
114
99
func locationsFromLimaYAML (y * limayaml.LimaYAML ) map [string ]limayaml.File {
115
100
locations := make (map [string ]limayaml.File )
116
101
for _ , f := range y .Images {
117
- locations [sha256OfURL (f .Location )] = f .File
102
+ locations [downloader . CacheKey (f .Location )] = f .File
118
103
if f .Kernel != nil {
119
- locations [sha256OfURL (f .Kernel .Location )] = f .Kernel .File
104
+ locations [downloader . CacheKey (f .Kernel .Location )] = f .Kernel .File
120
105
}
121
106
if f .Initrd != nil {
122
- locations [sha256OfURL (f .Initrd .Location )] = * f .Initrd
107
+ locations [downloader . CacheKey (f .Initrd .Location )] = * f .Initrd
123
108
}
124
109
}
125
110
for _ , f := range y .Containerd .Archives {
126
- locations [sha256OfURL (f .Location )] = f
111
+ locations [downloader . CacheKey (f .Location )] = f
127
112
}
128
113
for _ , f := range y .Firmware .Images {
129
- locations [sha256OfURL (f .Location )] = f .File
114
+ locations [downloader . CacheKey (f .Location )] = f .File
130
115
}
131
116
return locations
132
117
}
133
-
134
- func sha256OfURL (url string ) string {
135
- return fmt .Sprintf ("%x" , sha256 .Sum256 ([]byte (url )))
136
- }
0 commit comments