Skip to content

Commit 46a44c8

Browse files
authored
feat: support the progress writer for pull operation (#171)
Signed-off-by: chlins <[email protected]>
1 parent e08223e commit 46a44c8

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

internal/pb/pb.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package pb
1919
import (
2020
"fmt"
2121
"io"
22+
"os"
2223
"sync"
2324
"time"
2425

@@ -46,9 +47,24 @@ type progressBar struct {
4647
}
4748

4849
// NewProgressBar creates a new progress bar.
49-
func NewProgressBar() *ProgressBar {
50+
func NewProgressBar(writers ...io.Writer) *ProgressBar {
51+
opts := []mpbv8.ContainerOption{
52+
mpbv8.WithAutoRefresh(),
53+
mpbv8.WithWidth(60),
54+
mpbv8.WithRefreshRate(300 * time.Millisecond),
55+
}
56+
57+
// If no writer specified, use stdout.
58+
if len(writers) == 0 {
59+
opts = append(opts, mpbv8.WithOutput(os.Stdout))
60+
} else if len(writers) == 1 {
61+
opts = append(opts, mpbv8.WithOutput(writers[0]))
62+
} else {
63+
opts = append(opts, mpbv8.WithOutput(io.MultiWriter(writers...)))
64+
}
65+
5066
return &ProgressBar{
51-
mpb: mpbv8.New(mpbv8.WithWidth(60), mpbv8.WithRefreshRate(300*time.Millisecond)),
67+
mpb: mpbv8.New(opts...),
5268
bars: make(map[string]*progressBar),
5369
}
5470
}

pkg/backend/pull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (b *backend) Pull(ctx context.Context, target string, cfg *config.Pull) err
6060
}
6161

6262
// create the progress bar to track the progress of push.
63-
pb := internalpb.NewProgressBar()
63+
pb := internalpb.NewProgressBar(cfg.ProgressWriter)
6464
pb.Start()
6565
defer pb.Stop()
6666

pkg/config/pull.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package config
1818

1919
import (
2020
"fmt"
21+
"io"
22+
"os"
2123

2224
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
2325
)
@@ -35,6 +37,7 @@ type Pull struct {
3537
ExtractDir string
3638
ExtractFromRemote bool
3739
Hooks PullHooks
40+
ProgressWriter io.Writer
3841
}
3942

4043
func NewPull() *Pull {
@@ -46,6 +49,7 @@ func NewPull() *Pull {
4649
ExtractDir: "",
4750
ExtractFromRemote: false,
4851
Hooks: &emptyPullHook{},
52+
ProgressWriter: os.Stdout,
4953
}
5054
}
5155

0 commit comments

Comments
 (0)