@@ -12,6 +12,7 @@ import (
1212 "github.com/bcmi-labs/orchestrator/cmd/feedback"
1313 "github.com/bcmi-labs/orchestrator/cmd/i18n"
1414 "github.com/codeclysm/extract/v4"
15+ "github.com/schollz/progressbar/v3"
1516)
1617
1718type Manifest struct {
@@ -28,33 +29,6 @@ type Release struct {
2829// DownloadConfirmCB is a function that is called when a Debian image is ready to be downloaded.
2930type DownloadConfirmCB func (target string ) (bool , error )
3031
31- // PassThru wraps an existing io.Reader.
32- //
33- // It simply forwards the Read() call, while displaying
34- // the results from individual calls to it.
35- type PassThru struct {
36- io.Reader
37- total float64 // Total # of bytes transferred
38- length int64 // Expected length
39- progress float64
40- progressCB func (f float64 )
41- }
42-
43- // Read 'overrides' the underlying io.Reader's Read method.
44- // This is the one that will be called by io.Copy(). We simply
45- // use it to keep track of the progress and then forward the call.
46- func (pt * PassThru ) Read (p []byte ) (int , error ) {
47- n , err := pt .Reader .Read (p )
48- pt .total += float64 (n )
49- percentage := pt .total / float64 (pt .length ) * float64 (100 )
50- if percentage - pt .progress > 1 {
51- pt .progressCB (percentage )
52- pt .progress = percentage
53- }
54-
55- return n , err
56- }
57-
5832func DownloadAndExtract (client * Client , targetVersion string , upgradeConfirmCb DownloadConfirmCB , forceYes bool ) (* paths.Path , error ) {
5933 tmpZip , version , err := DownloadImage (client , targetVersion , upgradeConfirmCb , forceYes )
6034 if err != nil {
@@ -111,7 +85,6 @@ func DownloadImage(client *Client, targetVersion string, upgradeConfirmCb Downlo
11185 }
11286 }
11387
114- feedback .Print (i18n .Tr ("Downloading Debian image version %s" , rel .Version ))
11588 download , size , err := client .FetchZip (rel .Url )
11689 if err != nil {
11790 return nil , "" , fmt .Errorf ("could not fetch Debian image: %w" , err )
@@ -132,9 +105,12 @@ func DownloadImage(client *Client, targetVersion string, upgradeConfirmCb Downlo
132105 defer tmpZipFile .Close ()
133106
134107 // Download and keep track of the progress
135- src := & PassThru {Reader : download , length : size , progressCB : func (f float64 ) { feedback .Printf ("Download progress: %.2f %%" , f ) }}
108+ bar := progressbar .DefaultBytes (
109+ size ,
110+ i18n .Tr ("Downloading Debian image version %s" , rel .Version ),
111+ )
136112 checksum := sha256 .New ()
137- if _ , err := io .Copy (io .MultiWriter (checksum , tmpZipFile ), src ); err != nil {
113+ if _ , err := io .Copy (io .MultiWriter (checksum , tmpZipFile , bar ), download ); err != nil {
138114 return nil , "" , err
139115 }
140116
0 commit comments