Skip to content

Commit 9a94102

Browse files
authored
feat: improve download bar
1 parent 2a12fb0 commit 9a94102

File tree

3 files changed

+22
-30
lines changed

3 files changed

+22
-30
lines changed

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/arduino/go-windows-runas v1.0.1
88
github.com/bcmi-labs/orchestrator v0.3.0
99
github.com/codeclysm/extract/v4 v4.0.0
10+
github.com/schollz/progressbar/v3 v3.18.0
1011
github.com/spf13/cobra v1.9.1
1112
go.bug.st/cleanup v1.0.0
1213
)
@@ -16,8 +17,11 @@ require (
1617
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1718
github.com/juju/errors v1.0.0 // indirect
1819
github.com/klauspost/compress v1.18.0 // indirect
20+
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
21+
github.com/rivo/uniseg v0.4.7 // indirect
1922
github.com/sirupsen/logrus v1.9.3 // indirect
2023
github.com/spf13/pflag v1.0.7 // indirect
2124
github.com/ulikunitz/xz v0.5.12 // indirect
2225
golang.org/x/sys v0.36.0 // indirect
26+
golang.org/x/term v0.35.0 // indirect
2327
)

go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ github.com/arduino/go-windows-runas v1.0.1 h1:hkbjjkg3nlGHsSflYyg803N67T6NQygbSa
44
github.com/arduino/go-windows-runas v1.0.1/go.mod h1:cyVCsn7S6M3H1Nl94YOEu14ZJU+DYiPTDeYQcCMVWqk=
55
github.com/bcmi-labs/orchestrator v0.3.0 h1:lCsGiz1OlM8Bzflavaz1+9y1roUUkGfA4IKntm8HXl4=
66
github.com/bcmi-labs/orchestrator v0.3.0/go.mod h1:D2NWi/sJtJ9/hDxaeotfftHi+dXbXXwDWsySXR8k3Pg=
7+
github.com/chengxilo/virtualterm v1.0.4 h1:Z6IpERbRVlfB8WkOmtbHiDbBANU7cimRIof7mk9/PwM=
8+
github.com/chengxilo/virtualterm v1.0.4/go.mod h1:DyxxBZz/x1iqJjFxTFcr6/x+jSpqN0iwWCOK1q10rlY=
79
github.com/codeclysm/extract/v4 v4.0.0 h1:H87LFsUNaJTu2e/8p/oiuiUsOK/TaPQ5wxsjPnwPEIY=
810
github.com/codeclysm/extract/v4 v4.0.0/go.mod h1:SFju1lj6as7FvUgalpSct7torJE0zttbJUWtryPRG6s=
911
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
@@ -25,10 +27,18 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
2527
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
2628
github.com/leonelquinteros/gotext v1.7.2 h1:bDPndU8nt+/kRo1m4l/1OXiiy2v7Z7dfPQ9+YP7G1Mc=
2729
github.com/leonelquinteros/gotext v1.7.2/go.mod h1:9/haCkm5P7Jay1sxKDGJ5WIg4zkz8oZKw4ekNpALob8=
30+
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
31+
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
32+
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
33+
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
2834
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2935
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
3036
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
37+
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
38+
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
3139
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
40+
github.com/schollz/progressbar/v3 v3.18.0 h1:uXdoHABRFmNIjUfte/Ex7WtuyVslrw2wVPQmCN62HpA=
41+
github.com/schollz/progressbar/v3 v3.18.0/go.mod h1:IsO3lpbaGuzh8zIMzgY3+J8l4C8GjO0Y9S69eFvNsec=
3242
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
3343
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
3444
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
@@ -48,6 +58,8 @@ go.bug.st/cleanup v1.0.0/go.mod h1:EqVmTg2IBk4znLbPD28xne3abjsJftMdqqJEjhn70bk=
4858
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4959
golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k=
5060
golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
61+
golang.org/x/term v0.35.0 h1:bZBVKBudEyhRcajGcNc3jIfWPqV4y/Kt2XcoigOWtDQ=
62+
golang.org/x/term v0.35.0/go.mod h1:TPGtkTLesOwf2DE8CgVYiZinHAOuy5AYUYT1lENIZnA=
5163
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
5264
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
5365
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=

updater/download_image.go

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1718
type 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.
2930
type 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-
5832
func 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

Comments
 (0)