Skip to content

Commit 6b5f9c9

Browse files
authored
feat: support disable the progress bar (#190)
Signed-off-by: chlins <[email protected]>
1 parent 8534718 commit 6b5f9c9

File tree

5 files changed

+38
-9
lines changed

5 files changed

+38
-9
lines changed

cmd/root.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ import (
2424
"os/signal"
2525
"syscall"
2626

27-
"github.com/CloudNativeAI/modctl/cmd/modelfile"
28-
"github.com/CloudNativeAI/modctl/pkg/config"
29-
3027
"github.com/spf13/cobra"
3128
"github.com/spf13/viper"
29+
30+
"github.com/CloudNativeAI/modctl/cmd/modelfile"
31+
internalpb "github.com/CloudNativeAI/modctl/internal/pb"
32+
"github.com/CloudNativeAI/modctl/pkg/config"
3233
)
3334

3435
var rootConfig *config.Root
@@ -51,6 +52,9 @@ var rootCmd = &cobra.Command{
5152
}
5253
}()
5354
}
55+
56+
// TODO: need refactor as currently use a global flag to control the progress bar render.
57+
internalpb.SetDisableProgress(rootConfig.DisableProgress)
5458
return nil
5559
},
5660
}
@@ -83,6 +87,7 @@ func init() {
8387
flags.StringVar(&rootConfig.StoargeDir, "storage-dir", rootConfig.StoargeDir, "specify the storage directory for modctl")
8488
flags.BoolVar(&rootConfig.Pprof, "pprof", rootConfig.Pprof, "enable pprof")
8589
flags.StringVar(&rootConfig.PprofAddr, "pprof-addr", rootConfig.PprofAddr, "specify the address for pprof")
90+
flags.BoolVar(&rootConfig.DisableProgress, "no-progress", rootConfig.DisableProgress, "disable progress bar")
8691

8792
// Bind common flags.
8893
if err := viper.BindPFlags(flags); err != nil {

internal/pb/pb.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ import (
2828
"github.com/vbauerster/mpb/v8/decor"
2929
)
3030

31+
var (
32+
// disableProgress is the flag to disable progress bar.
33+
disableProgress bool
34+
)
35+
36+
// SetDisableProgress disables the progress bar.
37+
func SetDisableProgress(disable bool) {
38+
disableProgress = disable
39+
}
40+
3141
// NormalizePrompt normalizes the prompt string.
3242
func NormalizePrompt(prompt string) string {
3343
return fmt.Sprintf("%s =>", prompt)
@@ -71,6 +81,11 @@ func NewProgressBar(writers ...io.Writer) *ProgressBar {
7181

7282
// Add adds a new progress bar.
7383
func (p *ProgressBar) Add(prompt, name string, size int64, reader io.Reader) io.Reader {
84+
// Return the reader directly if progress is disabled.
85+
if disableProgress {
86+
return reader
87+
}
88+
7489
p.mu.RLock()
7590
oldBar := p.bars[name]
7691
p.mu.RUnlock()

pkg/backend/pull.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ func (b *backend) Pull(ctx context.Context, target string, cfg *config.Pull) err
5959
return fmt.Errorf("failed to decode the manifest: %w", err)
6060
}
6161

62+
// TODO: need refactor as currently use a global flag to control the progress bar render.
63+
if cfg.DisableProgress {
64+
internalpb.SetDisableProgress(true)
65+
}
66+
6267
// create the progress bar to track the progress of push.
6368
pb := internalpb.NewProgressBar(cfg.ProgressWriter)
6469
pb.Start()

pkg/config/pull.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type Pull struct {
3838
ExtractFromRemote bool
3939
Hooks PullHooks
4040
ProgressWriter io.Writer
41+
DisableProgress bool
4142
}
4243

4344
func NewPull() *Pull {
@@ -50,6 +51,7 @@ func NewPull() *Pull {
5051
ExtractFromRemote: false,
5152
Hooks: &emptyPullHook{},
5253
ProgressWriter: os.Stdout,
54+
DisableProgress: false,
5355
}
5456
}
5557

pkg/config/root.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ import (
2222
)
2323

2424
type Root struct {
25-
StoargeDir string
26-
Pprof bool
27-
PprofAddr string
25+
StoargeDir string
26+
Pprof bool
27+
PprofAddr string
28+
DisableProgress bool
2829
}
2930

3031
func NewRoot() (*Root, error) {
@@ -34,8 +35,9 @@ func NewRoot() (*Root, error) {
3435
}
3536

3637
return &Root{
37-
StoargeDir: filepath.Join(user.HomeDir, ".modctl"),
38-
Pprof: false,
39-
PprofAddr: "localhost:6060",
38+
StoargeDir: filepath.Join(user.HomeDir, ".modctl"),
39+
Pprof: false,
40+
PprofAddr: "localhost:6060",
41+
DisableProgress: false,
4042
}, nil
4143
}

0 commit comments

Comments
 (0)