Skip to content

Commit 217da28

Browse files
authored
Merge pull request #2908 from nirs/download-options
Extract options.apply helper
2 parents 77cbbda + 565fa0c commit 217da28

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

pkg/downloader/downloader.go

+20-18
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ type options struct {
5858
expectedDigest digest.Digest
5959
}
6060

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+
6170
type Opt func(*options) error
6271

6372
// WithCache enables caching using filepath.Join(os.UserCacheDir(), "lima") as the cache dir.
@@ -165,11 +174,10 @@ func readTime(path string) time.Time {
165174
// The local path can be an empty string for "caching only" mode.
166175
func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result, error) {
167176
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
172179
}
180+
173181
var localPath string
174182
if local == "" {
175183
if o.cacheDir == "" {
@@ -301,10 +309,8 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
301309
// When the cache path already exists, Cached returns Result with StatusUsedCache.
302310
func Cached(remote string, opts ...Opt) (*Result, error) {
303311
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
308314
}
309315
if o.cacheDir == "" {
310316
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 {
715721
// CacheEntries returns a map of cache entries.
716722
// The key is the SHA256 of the URL.
717723
// 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) {
719725
entries := make(map[string]string)
720726
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
725729
}
726730
if o.cacheDir == "" {
727731
return entries, nil
@@ -750,12 +754,10 @@ func CacheKey(remote string) string {
750754
}
751755

752756
// RemoveAllCacheDir removes the cache directory.
753-
func RemoveAllCacheDir(opt ...Opt) error {
757+
func RemoveAllCacheDir(opts ...Opt) error {
754758
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
759761
}
760762
if o.cacheDir == "" {
761763
return nil

0 commit comments

Comments
 (0)