Skip to content

Commit ce34ce1

Browse files
committed
Show a user-readable description of the download
Add a one-line url or description to the download progress bar. This allows knowing what the progress far, if not showing the log. Leave the long description in the log, consider moving it from INFO to DEBUG level. Otherwise show description and the base name. Signed-off-by: Anders F Björklund <[email protected]>
1 parent 4d5bca5 commit ce34ce1

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

pkg/downloader/downloader.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type Result struct {
3838

3939
type options struct {
4040
cacheDir string // default: empty (disables caching)
41+
description string // default: url
4142
expectedDigest digest.Digest
4243
}
4344

@@ -64,6 +65,14 @@ func WithCacheDir(cacheDir string) Opt {
6465
}
6566
}
6667

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+
6776
// WithExpectedDigest is used to validate the downloaded file against the expected digest.
6877
//
6978
// The digest is not verified in the following cases:
@@ -134,7 +143,7 @@ func Download(local, remote string, opts ...Opt) (*Result, error) {
134143
}
135144

136145
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 {
138147
return nil, err
139148
}
140149
res := &Result{
@@ -145,7 +154,7 @@ func Download(local, remote string, opts ...Opt) (*Result, error) {
145154
}
146155

147156
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 {
149158
return nil, err
150159
}
151160
res := &Result{
@@ -174,11 +183,11 @@ func Download(local, remote string, opts ...Opt) (*Result, error) {
174183
if o.expectedDigest.String() != shadDigestS {
175184
return nil, fmt.Errorf("expected digest %q does not match the cached digest %q", o.expectedDigest.String(), shadDigestS)
176185
}
177-
if err := copyLocal(localPath, shadData, ""); err != nil {
186+
if err := copyLocal(localPath, shadData, "", ""); err != nil {
178187
return nil, err
179188
}
180189
} else {
181-
if err := copyLocal(localPath, shadData, o.expectedDigest); err != nil {
190+
if err := copyLocal(localPath, shadData, o.description, o.expectedDigest); err != nil {
182191
return nil, err
183192
}
184193
}
@@ -199,11 +208,11 @@ func Download(local, remote string, opts ...Opt) (*Result, error) {
199208
if err := os.WriteFile(shadURL, []byte(remote), 0644); err != nil {
200209
return nil, err
201210
}
202-
if err := downloadHTTP(shadData, remote, o.expectedDigest); err != nil {
211+
if err := downloadHTTP(shadData, remote, o.description, o.expectedDigest); err != nil {
203212
return nil, err
204213
}
205214
// 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 {
207216
return nil, err
208217
}
209218
if shadDigest != "" && o.expectedDigest != "" {
@@ -244,7 +253,7 @@ func canonicalLocalPath(s string) (string, error) {
244253
return localpathutil.Expand(s)
245254
}
246255

247-
func copyLocal(dst, src string, expectedDigest digest.Digest) error {
256+
func copyLocal(dst, src string, description string, expectedDigest digest.Digest) error {
248257
srcPath, err := canonicalLocalPath(src)
249258
if err != nil {
250259
return err
@@ -262,6 +271,9 @@ func copyLocal(dst, src string, expectedDigest digest.Digest) error {
262271
if err != nil {
263272
return err
264273
}
274+
if description != "" {
275+
// TODO: progress bar for copy
276+
}
265277
return fs.CopyFile(dstPath, srcPath)
266278
}
267279

@@ -316,7 +328,7 @@ func createBar(size int64) (*pb.ProgressBar, error) {
316328
return bar, nil
317329
}
318330

319-
func downloadHTTP(localPath, url string, expectedDigest digest.Digest) error {
331+
func downloadHTTP(localPath, url string, description string, expectedDigest digest.Digest) error {
320332
if localPath == "" {
321333
return fmt.Errorf("downloadHTTP: got empty localPath")
322334
}
@@ -357,6 +369,12 @@ func downloadHTTP(localPath, url string, expectedDigest digest.Digest) error {
357369
}
358370
multiWriter := io.MultiWriter(writers...)
359371

372+
if !HideProgress {
373+
if description == "" {
374+
description = url
375+
}
376+
fmt.Printf("Downloading %s\n", description)
377+
}
360378
bar.Start()
361379
if _, err := io.Copy(multiWriter, bar.NewProxyReader(resp.Body)); err != nil {
362380
return err

pkg/fileutils/download.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package fileutils
22

33
import (
44
"fmt"
5+
"path"
56

67
"github.com/lima-vm/lima/pkg/downloader"
78
"github.com/lima-vm/lima/pkg/limayaml"
@@ -13,9 +14,11 @@ func DownloadFile(dest string, f limayaml.File, description string, expectedArch
1314
if f.Arch != expectedArch {
1415
return "", fmt.Errorf("unsupported arch: %q", f.Arch)
1516
}
16-
logrus.WithField("digest", f.Digest).Infof("Attempting to download %s from %q", description, f.Location)
17+
fields := logrus.Fields{"location": f.Location, "arch": f.Arch, "digest": f.Digest}
18+
logrus.WithFields(fields).Infof("Attempting to download %s", description)
1719
res, err := downloader.Download(dest, f.Location,
1820
downloader.WithCache(),
21+
downloader.WithDescription(fmt.Sprintf("%s (%s)", description, path.Base(f.Location))),
1922
downloader.WithExpectedDigest(f.Digest),
2023
)
2124
if err != nil {

0 commit comments

Comments
 (0)