Skip to content

Commit 82009e9

Browse files
committed
Add progress bar to example program
Most users will want to integrate a progress bar and this serve as a good example of how to do this. This also makes it more fun to work on this project. Ubuntu 24.04 image in qcow2 compressed format: % ./go-qcow2reader-example convert /var/tmp/images/test.zlib.qcow2 /tmp/tmp.img 6.00 GiB / 6.00 GiB [-------------------------------------] 100.00% 2.86 GiB p/s Ubuntu 24.04 image in qcow2 format: % ./go-qcow2reader-example convert /var/tmp/images/test.qcow2 /tmp/tmp.img 6.00 GiB / 6.00 GiB [------------------------------------] 100.00% 15.73 GiB p/s 16 TiB empty qcow2 image: % ./go-qcow2reader-example convert /var/tmp/images/test.0p.qcow2 /tmp/tmp.img 16.00 TiB / 16.00 TiB [---------------------------------] 100.00% 152.46 TiB p/s The pb module brings too many dependencies. Maybe we can find another library or write a very simple progress instead. Signed-off-by: Nir Soffer <[email protected]>
1 parent c3792ed commit 82009e9

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

cmd/go-qcow2reader-example/convert.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"os"
88

9+
"github.com/cheggaaa/pb/v3"
910
"github.com/lima-vm/go-qcow2reader"
1011
"github.com/lima-vm/go-qcow2reader/convert"
1112
"github.com/lima-vm/go-qcow2reader/log"
@@ -76,7 +77,12 @@ func cmdConvert(args []string) error {
7677
if err != nil {
7778
return err
7879
}
79-
if err := c.Convert(t, img, img.Size(), nil); err != nil {
80+
81+
bar := newProgressBar(img.Size())
82+
bar.Start()
83+
defer bar.Finish()
84+
85+
if err := c.Convert(t, img, img.Size(), bar); err != nil {
8086
return err
8187
}
8288

@@ -86,3 +92,18 @@ func cmdConvert(args []string) error {
8692

8793
return t.Close()
8894
}
95+
96+
// progressBar adapts pb.ProgressBar to the Updater interface.
97+
type progressBar struct {
98+
*pb.ProgressBar
99+
}
100+
101+
func newProgressBar(size int64) *progressBar {
102+
b := &progressBar{pb.New64(size)}
103+
b.Set(pb.Bytes, true)
104+
return b
105+
}
106+
107+
func (b *progressBar) Update(n int64) {
108+
b.ProgressBar.Add64(n)
109+
}

cmd/go-qcow2reader-example/go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,19 @@ module github.com/lima-vm/go-qcow2reader/cmd/go-qcow2reader-example
33
go 1.22
44

55
require (
6+
github.com/cheggaaa/pb/v3 v3.1.5
67
github.com/klauspost/compress v1.16.5
78
github.com/lima-vm/go-qcow2reader v0.0.0-00010101000000-000000000000
89
)
910

11+
require (
12+
github.com/VividCortex/ewma v1.2.0 // indirect
13+
github.com/fatih/color v1.15.0 // indirect
14+
github.com/mattn/go-colorable v0.1.13 // indirect
15+
github.com/mattn/go-isatty v0.0.19 // indirect
16+
github.com/mattn/go-runewidth v0.0.15 // indirect
17+
github.com/rivo/uniseg v0.2.0 // indirect
18+
golang.org/x/sys v0.6.0 // indirect
19+
)
20+
1021
replace github.com/lima-vm/go-qcow2reader => ../../

cmd/go-qcow2reader-example/go.sum

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,20 @@
1+
github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1ow=
2+
github.com/VividCortex/ewma v1.2.0/go.mod h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAUnGx7j5l4=
3+
github.com/cheggaaa/pb/v3 v3.1.5 h1:QuuUzeM2WsAqG2gMqtzaWithDJv0i+i6UlnwSCI4QLk=
4+
github.com/cheggaaa/pb/v3 v3.1.5/go.mod h1:CrxkeghYTXi1lQBEI7jSn+3svI3cuc19haAj6jM60XI=
5+
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
6+
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
17
github.com/klauspost/compress v1.16.5 h1:IFV2oUNUzZaz+XyusxpLzpzS8Pt5rh0Z16For/djlyI=
28
github.com/klauspost/compress v1.16.5/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
9+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
10+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
11+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
12+
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
13+
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
14+
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
15+
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
16+
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
17+
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
18+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
19+
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
20+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

0 commit comments

Comments
 (0)