@@ -38,6 +38,7 @@ type Result struct {
38
38
39
39
type options struct {
40
40
cacheDir string // default: empty (disables caching)
41
+ description string // default: url
41
42
expectedDigest digest.Digest
42
43
}
43
44
@@ -64,6 +65,14 @@ func WithCacheDir(cacheDir string) Opt {
64
65
}
65
66
}
66
67
68
+ // WithDecription adds a user description of the download.
69
+ func WithDescription (description string ) Opt {
70
+ return func (o * options ) error {
71
+ o .description = description
72
+ return nil
73
+ }
74
+ }
75
+
67
76
// WithExpectedDigest is used to validate the downloaded file against the expected digest.
68
77
//
69
78
// The digest is not verified in the following cases:
@@ -134,7 +143,7 @@ func Download(local, remote string, opts ...Opt) (*Result, error) {
134
143
}
135
144
136
145
if IsLocal (remote ) {
137
- if err := copyLocal (localPath , remote , o .expectedDigest ); err != nil {
146
+ if err := copyLocal (localPath , remote , o .description , o . expectedDigest ); err != nil {
138
147
return nil , err
139
148
}
140
149
res := & Result {
@@ -145,7 +154,7 @@ func Download(local, remote string, opts ...Opt) (*Result, error) {
145
154
}
146
155
147
156
if o .cacheDir == "" {
148
- if err := downloadHTTP (localPath , remote , o .expectedDigest ); err != nil {
157
+ if err := downloadHTTP (localPath , remote , o .description , o . expectedDigest ); err != nil {
149
158
return nil , err
150
159
}
151
160
res := & Result {
@@ -174,11 +183,11 @@ func Download(local, remote string, opts ...Opt) (*Result, error) {
174
183
if o .expectedDigest .String () != shadDigestS {
175
184
return nil , fmt .Errorf ("expected digest %q does not match the cached digest %q" , o .expectedDigest .String (), shadDigestS )
176
185
}
177
- if err := copyLocal (localPath , shadData , "" ); err != nil {
186
+ if err := copyLocal (localPath , shadData , "" , "" ); err != nil {
178
187
return nil , err
179
188
}
180
189
} else {
181
- if err := copyLocal (localPath , shadData , o .expectedDigest ); err != nil {
190
+ if err := copyLocal (localPath , shadData , o .description , o . expectedDigest ); err != nil {
182
191
return nil , err
183
192
}
184
193
}
@@ -199,11 +208,11 @@ func Download(local, remote string, opts ...Opt) (*Result, error) {
199
208
if err := os .WriteFile (shadURL , []byte (remote ), 0644 ); err != nil {
200
209
return nil , err
201
210
}
202
- if err := downloadHTTP (shadData , remote , o .expectedDigest ); err != nil {
211
+ if err := downloadHTTP (shadData , remote , o .description , o . expectedDigest ); err != nil {
203
212
return nil , err
204
213
}
205
214
// no need to pass the digest to copyLocal(), as we already verified the digest
206
- if err := copyLocal (localPath , shadData , "" ); err != nil {
215
+ if err := copyLocal (localPath , shadData , "" , "" ); err != nil {
207
216
return nil , err
208
217
}
209
218
if shadDigest != "" && o .expectedDigest != "" {
@@ -244,7 +253,7 @@ func canonicalLocalPath(s string) (string, error) {
244
253
return localpathutil .Expand (s )
245
254
}
246
255
247
- func copyLocal (dst , src string , expectedDigest digest.Digest ) error {
256
+ func copyLocal (dst , src string , description string , expectedDigest digest.Digest ) error {
248
257
srcPath , err := canonicalLocalPath (src )
249
258
if err != nil {
250
259
return err
@@ -262,6 +271,9 @@ func copyLocal(dst, src string, expectedDigest digest.Digest) error {
262
271
if err != nil {
263
272
return err
264
273
}
274
+ if description != "" {
275
+ // TODO: progress bar for copy
276
+ }
265
277
return fs .CopyFile (dstPath , srcPath )
266
278
}
267
279
@@ -316,7 +328,7 @@ func createBar(size int64) (*pb.ProgressBar, error) {
316
328
return bar , nil
317
329
}
318
330
319
- func downloadHTTP (localPath , url string , expectedDigest digest.Digest ) error {
331
+ func downloadHTTP (localPath , url string , description string , expectedDigest digest.Digest ) error {
320
332
if localPath == "" {
321
333
return fmt .Errorf ("downloadHTTP: got empty localPath" )
322
334
}
@@ -357,6 +369,12 @@ func downloadHTTP(localPath, url string, expectedDigest digest.Digest) error {
357
369
}
358
370
multiWriter := io .MultiWriter (writers ... )
359
371
372
+ if ! HideProgress {
373
+ if description == "" {
374
+ description = url
375
+ }
376
+ fmt .Printf ("Downloading %s\n " , description )
377
+ }
360
378
bar .Start ()
361
379
if _ , err := io .Copy (multiWriter , bar .NewProxyReader (resp .Body )); err != nil {
362
380
return err
0 commit comments