@@ -58,6 +58,15 @@ type options struct {
58
58
expectedDigest digest.Digest
59
59
}
60
60
61
+ func (o * options ) apply (opts []Opt ) error {
62
+ for _ , f := range opts {
63
+ if err := f (o ); err != nil {
64
+ return err
65
+ }
66
+ }
67
+ return nil
68
+ }
69
+
61
70
type Opt func (* options ) error
62
71
63
72
// WithCache enables caching using filepath.Join(os.UserCacheDir(), "lima") as the cache dir.
@@ -165,11 +174,10 @@ func readTime(path string) time.Time {
165
174
// The local path can be an empty string for "caching only" mode.
166
175
func Download (ctx context.Context , local , remote string , opts ... Opt ) (* Result , error ) {
167
176
var o options
168
- for _ , f := range opts {
169
- if err := f (& o ); err != nil {
170
- return nil , err
171
- }
177
+ if err := o .apply (opts ); err != nil {
178
+ return nil , err
172
179
}
180
+
173
181
var localPath string
174
182
if local == "" {
175
183
if o .cacheDir == "" {
@@ -301,10 +309,8 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
301
309
// When the cache path already exists, Cached returns Result with StatusUsedCache.
302
310
func Cached (remote string , opts ... Opt ) (* Result , error ) {
303
311
var o options
304
- for _ , f := range opts {
305
- if err := f (& o ); err != nil {
306
- return nil , err
307
- }
312
+ if err := o .apply (opts ); err != nil {
313
+ return nil , err
308
314
}
309
315
if o .cacheDir == "" {
310
316
return nil , fmt .Errorf ("caching-only mode requires the cache directory to be specified" )
@@ -715,13 +721,11 @@ func writeFirst(path string, data []byte, perm os.FileMode) error {
715
721
// CacheEntries returns a map of cache entries.
716
722
// The key is the SHA256 of the URL.
717
723
// The value is the path to the cache entry.
718
- func CacheEntries (opt ... Opt ) (map [string ]string , error ) {
724
+ func CacheEntries (opts ... Opt ) (map [string ]string , error ) {
719
725
entries := make (map [string ]string )
720
726
var o options
721
- for _ , f := range opt {
722
- if err := f (& o ); err != nil {
723
- return nil , err
724
- }
727
+ if err := o .apply (opts ); err != nil {
728
+ return nil , err
725
729
}
726
730
if o .cacheDir == "" {
727
731
return entries , nil
@@ -750,12 +754,10 @@ func CacheKey(remote string) string {
750
754
}
751
755
752
756
// RemoveAllCacheDir removes the cache directory.
753
- func RemoveAllCacheDir (opt ... Opt ) error {
757
+ func RemoveAllCacheDir (opts ... Opt ) error {
754
758
var o options
755
- for _ , f := range opt {
756
- if err := f (& o ); err != nil {
757
- return err
758
- }
759
+ if err := o .apply (opts ); err != nil {
760
+ return err
759
761
}
760
762
if o .cacheDir == "" {
761
763
return nil
0 commit comments