mirror of
https://github.com/restic/restic.git
synced 2026-06-17 22:24:17 +00:00
stats: hide progress bar for json output (#21871)
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
Bugfix: Hide progress bar for stats command in JSON mode
|
||||
|
||||
Since restic 0.19.0, the stats command shows a progress bar. However,
|
||||
it was also active when given the `--json` option resulting in text
|
||||
mixed with JSON. This has been fixed.
|
||||
|
||||
https://github.com/restic/restic/issues/21866
|
||||
https://github.com/restic/restic/pull/21871
|
||||
@@ -141,7 +141,7 @@ func runStats(ctx context.Context, opts StatsOptions, gopts global.Options, args
|
||||
snapshots = append(snapshots, sn)
|
||||
}
|
||||
|
||||
statsProgress := newStatsProgress(term, uint64(len(snapshots)))
|
||||
statsProgress := newStatsProgress(term, !gopts.JSON, uint64(len(snapshots)))
|
||||
|
||||
updater := progress.NewUpdater(ui.CalculateProgressInterval(!gopts.Quiet, gopts.JSON, term.CanUpdateStatus()), func(runtime time.Duration, final bool) {
|
||||
statsProgress.printProgress(runtime, final)
|
||||
@@ -375,6 +375,7 @@ type statsProgress struct {
|
||||
term ui.Terminal
|
||||
m sync.Mutex
|
||||
snapshotCount uint64
|
||||
show bool
|
||||
|
||||
processedSnapshotCount uint64
|
||||
processedFileCount uint64
|
||||
@@ -382,14 +383,18 @@ type statsProgress struct {
|
||||
processedSize uint64
|
||||
}
|
||||
|
||||
func newStatsProgress(term ui.Terminal, snapshotCount uint64) *statsProgress {
|
||||
func newStatsProgress(term ui.Terminal, show bool, snapshotCount uint64) *statsProgress {
|
||||
return &statsProgress{
|
||||
term: term,
|
||||
show: show,
|
||||
snapshotCount: snapshotCount,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *statsProgress) printProgress(runtime time.Duration, final bool) {
|
||||
if !s.show {
|
||||
return
|
||||
}
|
||||
s.m.Lock()
|
||||
|
||||
progressBase := s.processedSnapshotCount
|
||||
|
||||
@@ -66,7 +66,7 @@ func TestSizeHistogramString(t *testing.T) {
|
||||
func TestStatsProgress(t *testing.T) {
|
||||
term := &ui.MockTerminal{}
|
||||
|
||||
progress := newStatsProgress(term, 2)
|
||||
progress := newStatsProgress(term, true, 2)
|
||||
progress.printProgress(0*time.Second, false)
|
||||
rtest.Equals(t, []string{"[0:00] 0.00% 0 / 2 snapshots, 0 B"}, term.Output)
|
||||
|
||||
@@ -88,3 +88,12 @@ func TestStatsProgress(t *testing.T) {
|
||||
progress.printProgress(20*time.Second, true)
|
||||
rtest.Equals(t, []string{"[0:20] 100.00% 2 / 2 snapshots, 4 files, 5 blobs, 6 B"}, term.Output)
|
||||
}
|
||||
|
||||
func TestStatsProgressJSON(t *testing.T) {
|
||||
term := &ui.MockTerminal{}
|
||||
|
||||
progress := newStatsProgress(term, false, 2)
|
||||
progress.printProgress(0*time.Second, false)
|
||||
// JSON output is not available yet, so just make sure to not break normal json output
|
||||
rtest.Equals(t, nil, term.Output)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user