Skip to content

Commit 8178a1b

Browse files
cfergeaupraveenkumar
authored andcommitted
download: Improve progress bar
The current progress information for downloads is a bit too verbose: Downloading 1 files... Downloading /home/teuf/.crc/cache/crc_libvirt_4.9.0.crcbundle 505MB / 2GB (17%) - 10.07MB/s ETA: 4m12s It is also inconsistent with the progress information used for bundle extraction. This commit switches the download code to use github.com/cheggaaa/pb/v3 for progress, which is the same module as the one used by the extract package.
1 parent 0a882d3 commit 8178a1b

4 files changed

Lines changed: 32 additions & 207 deletions

File tree

pkg/download/download.go

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,69 @@
11
package download
22

33
import (
4-
"context"
54
"crypto/sha256"
65
"encoding/hex"
76
"net/http"
87
"os"
8+
"time"
99

1010
"github.com/code-ready/crc/pkg/crc/logging"
1111
"github.com/code-ready/crc/pkg/crc/network"
1212

1313
"github.com/cavaliercoder/grab"
14-
"github.com/cavaliercoder/grab/grabui"
14+
"github.com/cheggaaa/pb/v3"
1515
"github.com/pkg/errors"
1616
)
1717

18+
func doRequest(client *grab.Client, req *grab.Request) (string, error) {
19+
resp := client.Do(req)
20+
21+
t := time.NewTicker(500 * time.Millisecond)
22+
defer t.Stop()
23+
bar := pb.Start64(resp.Size())
24+
bar.Set(pb.Bytes, true)
25+
defer bar.Finish()
26+
27+
loop:
28+
for {
29+
select {
30+
case <-t.C:
31+
bar.SetCurrent(resp.BytesComplete())
32+
case <-resp.Done:
33+
break loop
34+
}
35+
}
36+
37+
return resp.Filename, resp.Err()
38+
}
39+
1840
// Download function takes sha256sum as hex decoded byte
1941
// something like hex.DecodeString("33daf4c03f86120fdfdc66bddf6bfff4661c7ca11c5d")
2042
func Download(uri, destination string, mode os.FileMode, sha256sum []byte) (string, error) {
2143
logging.Debugf("Downloading %s to %s", uri, destination)
2244

2345
client := grab.NewClient()
2446
client.HTTPClient = &http.Client{Transport: network.HTTPTransport()}
25-
consoleClient := grabui.NewConsoleClient(client)
2647
req, err := grab.NewRequest(destination, uri)
2748
if err != nil {
28-
return "", errors.Wrapf(err, "unable to get response from %s", uri)
49+
return "", errors.Wrapf(err, "unable to get request from %s", uri)
2950
}
3051
if sha256sum != nil {
3152
req.SetChecksum(sha256.New(), sha256sum, true)
3253
}
3354

34-
respCh := consoleClient.Do(context.Background(), 3, req)
35-
resp := <-respCh
36-
37-
if resp.Err() != nil {
38-
return "", resp.Err()
55+
filename, err := doRequest(client, req)
56+
if err != nil {
57+
return "", err
3958
}
4059

41-
if err := os.Chmod(resp.Filename, mode); err != nil {
42-
_ = os.Remove(resp.Filename)
60+
if err := os.Chmod(filename, mode); err != nil {
61+
_ = os.Remove(filename)
4362
return "", err
4463
}
4564

46-
logging.Debugf("Download saved to %v", resp.Filename)
47-
return resp.Filename, nil
65+
logging.Debugf("Download saved to %v", filename)
66+
return filename, nil
4867
}
4968

5069
type RemoteFile struct {

vendor/github.com/cavaliercoder/grab/grabui/console_client.go

Lines changed: 0 additions & 166 deletions
This file was deleted.

vendor/github.com/cavaliercoder/grab/grabui/grabui.go

Lines changed: 0 additions & 27 deletions
This file was deleted.

vendor/modules.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ github.com/asaskevich/govalidator
5555
## explicit
5656
github.com/cavaliercoder/grab
5757
github.com/cavaliercoder/grab/bps
58-
github.com/cavaliercoder/grab/grabui
5958
# github.com/cheggaaa/pb/v3 v3.0.8
6059
## explicit
6160
github.com/cheggaaa/pb/v3

0 commit comments

Comments
 (0)