diff --git a/changelog/unreleased/issue-21866 b/changelog/unreleased/issue-21866 new file mode 100644 index 000000000..815a472c1 --- /dev/null +++ b/changelog/unreleased/issue-21866 @@ -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 diff --git a/cmd/restic/cmd_stats.go b/cmd/restic/cmd_stats.go index f2b793bbc..d09f97ecb 100644 --- a/cmd/restic/cmd_stats.go +++ b/cmd/restic/cmd_stats.go @@ -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 diff --git a/cmd/restic/cmd_stats_test.go b/cmd/restic/cmd_stats_test.go index 231bd65ff..a2068de44 100644 --- a/cmd/restic/cmd_stats_test.go +++ b/cmd/restic/cmd_stats_test.go @@ -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) +}