move NewProgressPrinter to ui package

This commit is contained in:
Michael Eischer
2025-09-14 19:42:33 +02:00
parent ff5a0cc851
commit e753941ad3
39 changed files with 151 additions and 148 deletions

View File

@@ -478,7 +478,7 @@ func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, ter
var vsscfg fs.VSSConfig var vsscfg fs.VSSConfig
var err error var err error
msg := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, term) msg := ui.NewProgressPrinter(gopts.JSON, gopts.verbosity, term)
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
if vsscfg, err = fs.ParseVSSConfig(gopts.extended); err != nil { if vsscfg, err = fs.ParseVSSConfig(gopts.extended); err != nil {
return err return err
@@ -521,7 +521,7 @@ func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, ter
progressPrinter = backup.NewTextProgress(term, gopts.verbosity) progressPrinter = backup.NewTextProgress(term, gopts.verbosity)
} }
progressReporter := backup.NewProgress(progressPrinter, progressReporter := backup.NewProgress(progressPrinter,
calculateProgressInterval(!gopts.Quiet, gopts.JSON, term.CanUpdateStatus())) ui.CalculateProgressInterval(!gopts.Quiet, gopts.JSON, term.CanUpdateStatus()))
defer progressReporter.Done() defer progressReporter.Done()
// rejectByNameFuncs collect functions that can reject items from the backup based on path only // rejectByNameFuncs collect functions that can reject items from the backup based on path only
@@ -550,7 +550,7 @@ func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, ter
progressPrinter.V("load index files") progressPrinter.V("load index files")
} }
bar := newIndexTerminalProgress(msg) bar := ui.NewIndexCounter(msg)
err = repo.LoadIndex(ctx, bar) err = repo.LoadIndex(ctx, bar)
if err != nil { if err != nil {
return err return err

View File

@@ -56,7 +56,7 @@ func (opts *CacheOptions) AddFlags(f *pflag.FlagSet) {
} }
func runCache(opts CacheOptions, gopts GlobalOptions, args []string, term ui.Terminal) error { func runCache(opts CacheOptions, gopts GlobalOptions, args []string, term ui.Terminal) error {
printer := newTerminalProgressPrinter(false, gopts.verbosity, term) printer := ui.NewProgressPrinter(false, gopts.verbosity, term)
if len(args) > 0 { if len(args) > 0 {
return errors.Fatal("the cache command expects no arguments, only options - please see `restic help cache` for usage and flags") return errors.Fatal("the cache command expects no arguments, only options - please see `restic help cache` for usage and flags")

View File

@@ -65,7 +65,7 @@ func validateCatArgs(args []string) error {
} }
func runCat(ctx context.Context, gopts GlobalOptions, args []string, term ui.Terminal) error { func runCat(ctx context.Context, gopts GlobalOptions, args []string, term ui.Terminal) error {
printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, term) printer := ui.NewProgressPrinter(gopts.JSON, gopts.verbosity, term)
if err := validateCatArgs(args); err != nil { if err := validateCatArgs(args); err != nil {
return err return err
@@ -168,7 +168,7 @@ func runCat(ctx context.Context, gopts GlobalOptions, args []string, term ui.Ter
return err return err
case "blob": case "blob":
bar := newIndexTerminalProgress(printer) bar := ui.NewIndexCounter(printer)
err = repo.LoadIndex(ctx, bar) err = repo.LoadIndex(ctx, bar)
if err != nil { if err != nil {
return err return err
@@ -196,7 +196,7 @@ func runCat(ctx context.Context, gopts GlobalOptions, args []string, term ui.Ter
return errors.Fatalf("could not find snapshot: %v", err) return errors.Fatalf("could not find snapshot: %v", err)
} }
bar := newIndexTerminalProgress(printer) bar := ui.NewIndexCounter(printer)
err = repo.LoadIndex(ctx, bar) err = repo.LoadIndex(ctx, bar)
if err != nil { if err != nil {
return err return err

View File

@@ -225,7 +225,7 @@ func runCheck(ctx context.Context, opts CheckOptions, gopts GlobalOptions, args
var printer progress.Printer var printer progress.Printer
if !gopts.JSON { if !gopts.JSON {
printer = newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, term) printer = ui.NewProgressPrinter(gopts.JSON, gopts.verbosity, term)
} else { } else {
printer = newJSONErrorPrinter(term) printer = newJSONErrorPrinter(term)
} }
@@ -249,7 +249,7 @@ func runCheck(ctx context.Context, opts CheckOptions, gopts GlobalOptions, args
} }
printer.P("load indexes\n") printer.P("load indexes\n")
bar := newIndexTerminalProgress(printer) bar := ui.NewIndexCounter(printer)
hints, errs := chkr.LoadIndex(ctx, bar) hints, errs := chkr.LoadIndex(ctx, bar)
if ctx.Err() != nil { if ctx.Err() != nil {
return summary, ctx.Err() return summary, ctx.Err()

View File

@@ -68,7 +68,7 @@ func (opts *CopyOptions) AddFlags(f *pflag.FlagSet) {
} }
func runCopy(ctx context.Context, opts CopyOptions, gopts GlobalOptions, args []string, term ui.Terminal) error { func runCopy(ctx context.Context, opts CopyOptions, gopts GlobalOptions, args []string, term ui.Terminal) error {
printer := newTerminalProgressPrinter(false, gopts.verbosity, term) printer := ui.NewProgressPrinter(false, gopts.verbosity, term)
secondaryGopts, isFromRepo, err := fillSecondaryGlobalOpts(ctx, opts.secondaryRepoOptions, gopts, "destination", printer) secondaryGopts, isFromRepo, err := fillSecondaryGlobalOpts(ctx, opts.secondaryRepoOptions, gopts, "destination", printer)
if err != nil { if err != nil {
return err return err
@@ -101,11 +101,11 @@ func runCopy(ctx context.Context, opts CopyOptions, gopts GlobalOptions, args []
} }
debug.Log("Loading source index") debug.Log("Loading source index")
bar := newIndexTerminalProgress(printer) bar := ui.NewIndexCounter(printer)
if err := srcRepo.LoadIndex(ctx, bar); err != nil { if err := srcRepo.LoadIndex(ctx, bar); err != nil {
return err return err
} }
bar = newIndexTerminalProgress(printer) bar = ui.NewIndexCounter(printer)
debug.Log("Loading destination index") debug.Log("Loading destination index")
if err := dstRepo.LoadIndex(ctx, bar); err != nil { if err := dstRepo.LoadIndex(ctx, bar); err != nil {
return err return err

View File

@@ -184,7 +184,7 @@ func dumpIndexes(ctx context.Context, repo restic.ListerLoaderUnpacked, wr io.Wr
} }
func runDebugDump(ctx context.Context, gopts GlobalOptions, args []string, term ui.Terminal) error { func runDebugDump(ctx context.Context, gopts GlobalOptions, args []string, term ui.Terminal) error {
printer := newTerminalProgressPrinter(false, gopts.verbosity, term) printer := ui.NewProgressPrinter(false, gopts.verbosity, term)
if len(args) != 1 { if len(args) != 1 {
return errors.Fatal("type not specified") return errors.Fatal("type not specified")
@@ -455,7 +455,7 @@ func storePlainBlob(id restic.ID, prefix string, plain []byte, printer progress.
} }
func runDebugExamine(ctx context.Context, gopts GlobalOptions, opts DebugExamineOptions, args []string, term ui.Terminal) error { func runDebugExamine(ctx context.Context, gopts GlobalOptions, opts DebugExamineOptions, args []string, term ui.Terminal) error {
printer := newTerminalProgressPrinter(false, gopts.verbosity, term) printer := ui.NewProgressPrinter(false, gopts.verbosity, term)
if opts.ExtractPack && gopts.NoLock { if opts.ExtractPack && gopts.NoLock {
return fmt.Errorf("--extract-pack and --no-lock are mutually exclusive") return fmt.Errorf("--extract-pack and --no-lock are mutually exclusive")
@@ -484,7 +484,7 @@ func runDebugExamine(ctx context.Context, gopts GlobalOptions, opts DebugExamine
return errors.Fatal("no pack files to examine") return errors.Fatal("no pack files to examine")
} }
bar := newIndexTerminalProgress(printer) bar := ui.NewIndexCounter(printer)
err = repo.LoadIndex(ctx, bar) err = repo.LoadIndex(ctx, bar)
if err != nil { if err != nil {
return err return err

View File

@@ -365,7 +365,7 @@ func runDiff(ctx context.Context, opts DiffOptions, gopts GlobalOptions, args []
return errors.Fatalf("specify two snapshot IDs") return errors.Fatalf("specify two snapshot IDs")
} }
printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, term) printer := ui.NewProgressPrinter(gopts.JSON, gopts.verbosity, term)
ctx, repo, unlock, err := openWithReadLock(ctx, gopts, gopts.NoLock, printer) ctx, repo, unlock, err := openWithReadLock(ctx, gopts, gopts.NoLock, printer)
if err != nil { if err != nil {
@@ -391,7 +391,7 @@ func runDiff(ctx context.Context, opts DiffOptions, gopts GlobalOptions, args []
if !gopts.JSON { if !gopts.JSON {
printer.P("comparing snapshot %v to %v:\n\n", sn1.ID().Str(), sn2.ID().Str()) printer.P("comparing snapshot %v to %v:\n\n", sn1.ID().Str(), sn2.ID().Str())
} }
bar := newIndexTerminalProgress(printer) bar := ui.NewIndexCounter(printer)
if err = repo.LoadIndex(ctx, bar); err != nil { if err = repo.LoadIndex(ctx, bar); err != nil {
return err return err
} }

View File

@@ -130,7 +130,7 @@ func runDump(ctx context.Context, opts DumpOptions, gopts GlobalOptions, args []
return errors.Fatal("no file and no snapshot ID specified") return errors.Fatal("no file and no snapshot ID specified")
} }
printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, term) printer := ui.NewProgressPrinter(gopts.JSON, gopts.verbosity, term)
switch opts.Archive { switch opts.Archive {
case "tar", "zip": case "tar", "zip":
@@ -160,7 +160,7 @@ func runDump(ctx context.Context, opts DumpOptions, gopts GlobalOptions, args []
return errors.Fatalf("failed to find snapshot: %v", err) return errors.Fatalf("failed to find snapshot: %v", err)
} }
bar := newIndexTerminalProgress(printer) bar := ui.NewIndexCounter(printer)
err = repo.LoadIndex(ctx, bar) err = repo.LoadIndex(ctx, bar)
if err != nil { if err != nil {
return err return err

View File

@@ -582,7 +582,7 @@ func runFind(ctx context.Context, opts FindOptions, gopts GlobalOptions, args []
return errors.Fatal("wrong number of arguments") return errors.Fatal("wrong number of arguments")
} }
printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, term) printer := ui.NewProgressPrinter(gopts.JSON, gopts.verbosity, term)
var err error var err error
pat := findPattern{pattern: args} pat := findPattern{pattern: args}
@@ -623,7 +623,7 @@ func runFind(ctx context.Context, opts FindOptions, gopts GlobalOptions, args []
if err != nil { if err != nil {
return err return err
} }
bar := newIndexTerminalProgress(printer) bar := ui.NewIndexCounter(printer)
if err = repo.LoadIndex(ctx, bar); err != nil { if err = repo.LoadIndex(ctx, bar); err != nil {
return err return err
} }

View File

@@ -186,7 +186,7 @@ func runForget(ctx context.Context, opts ForgetOptions, pruneOptions PruneOption
return errors.Fatal("--no-lock is only applicable in combination with --dry-run for forget command") return errors.Fatal("--no-lock is only applicable in combination with --dry-run for forget command")
} }
printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, term) printer := ui.NewProgressPrinter(gopts.JSON, gopts.verbosity, term)
ctx, repo, unlock, err := openWithExclusiveLock(ctx, gopts, opts.DryRun && gopts.NoLock, printer) ctx, repo, unlock, err := openWithExclusiveLock(ctx, gopts, opts.DryRun && gopts.NoLock, printer)
if err != nil { if err != nil {
return err return err

View File

@@ -115,7 +115,7 @@ func runGenerate(opts generateOptions, gopts GlobalOptions, args []string, term
return errors.Fatal("the generate command expects no arguments, only options - please see `restic help generate` for usage and flags") return errors.Fatal("the generate command expects no arguments, only options - please see `restic help generate` for usage and flags")
} }
printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, term) printer := ui.NewProgressPrinter(gopts.JSON, gopts.verbosity, term)
cmdRoot := newRootCommand(&GlobalOptions{}) cmdRoot := newRootCommand(&GlobalOptions{})
if opts.ManDir != "" { if opts.ManDir != "" {

View File

@@ -60,7 +60,7 @@ func runInit(ctx context.Context, opts InitOptions, gopts GlobalOptions, args []
return errors.Fatal("the init command expects no arguments, only options - please see `restic help init` for usage and flags") return errors.Fatal("the init command expects no arguments, only options - please see `restic help init` for usage and flags")
} }
printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, term) printer := ui.NewProgressPrinter(gopts.JSON, gopts.verbosity, term)
var version uint var version uint
switch opts.RepositoryVersion { switch opts.RepositoryVersion {

View File

@@ -59,7 +59,7 @@ func runKeyAdd(ctx context.Context, gopts GlobalOptions, opts KeyAddOptions, arg
return fmt.Errorf("the key add command expects no arguments, only options - please see `restic help key add` for usage and flags") return fmt.Errorf("the key add command expects no arguments, only options - please see `restic help key add` for usage and flags")
} }
printer := newTerminalProgressPrinter(false, gopts.verbosity, term) printer := ui.NewProgressPrinter(false, gopts.verbosity, term)
ctx, repo, unlock, err := openWithAppendLock(ctx, gopts, false, printer) ctx, repo, unlock, err := openWithAppendLock(ctx, gopts, false, printer)
if err != nil { if err != nil {
return err return err

View File

@@ -45,7 +45,7 @@ func runKeyList(ctx context.Context, gopts GlobalOptions, args []string, term ui
return fmt.Errorf("the key list command expects no arguments, only options - please see `restic help key list` for usage and flags") return fmt.Errorf("the key list command expects no arguments, only options - please see `restic help key list` for usage and flags")
} }
printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, term) printer := ui.NewProgressPrinter(gopts.JSON, gopts.verbosity, term)
ctx, repo, unlock, err := openWithReadLock(ctx, gopts, gopts.NoLock, printer) ctx, repo, unlock, err := openWithReadLock(ctx, gopts, gopts.NoLock, printer)
if err != nil { if err != nil {
return err return err

View File

@@ -54,7 +54,7 @@ func runKeyPasswd(ctx context.Context, gopts GlobalOptions, opts KeyPasswdOption
return fmt.Errorf("the key passwd command expects no arguments, only options - please see `restic help key passwd` for usage and flags") return fmt.Errorf("the key passwd command expects no arguments, only options - please see `restic help key passwd` for usage and flags")
} }
printer := newTerminalProgressPrinter(false, gopts.verbosity, term) printer := ui.NewProgressPrinter(false, gopts.verbosity, term)
ctx, repo, unlock, err := openWithExclusiveLock(ctx, gopts, false, printer) ctx, repo, unlock, err := openWithExclusiveLock(ctx, gopts, false, printer)
if err != nil { if err != nil {
return err return err

View File

@@ -42,7 +42,7 @@ func runKeyRemove(ctx context.Context, gopts GlobalOptions, args []string, term
return fmt.Errorf("key remove expects one argument as the key id") return fmt.Errorf("key remove expects one argument as the key id")
} }
printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, term) printer := ui.NewProgressPrinter(gopts.JSON, gopts.verbosity, term)
ctx, repo, unlock, err := openWithExclusiveLock(ctx, gopts, false, printer) ctx, repo, unlock, err := openWithExclusiveLock(ctx, gopts, false, printer)
if err != nil { if err != nil {
return err return err

View File

@@ -43,7 +43,7 @@ Exit status is 12 if the password is incorrect.
} }
func runList(ctx context.Context, gopts GlobalOptions, args []string, term ui.Terminal) error { func runList(ctx context.Context, gopts GlobalOptions, args []string, term ui.Terminal) error {
printer := newTerminalProgressPrinter(false, gopts.verbosity, term) printer := ui.NewProgressPrinter(false, gopts.verbosity, term)
if len(args) != 1 { if len(args) != 1 {
return errors.Fatal("type not specified") return errors.Fatal("type not specified")

View File

@@ -302,7 +302,7 @@ type toSortOutput struct {
} }
func runLs(ctx context.Context, opts LsOptions, gopts GlobalOptions, args []string, term ui.Terminal) error { func runLs(ctx context.Context, opts LsOptions, gopts GlobalOptions, args []string, term ui.Terminal) error {
termPrinter := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, term) termPrinter := ui.NewProgressPrinter(gopts.JSON, gopts.verbosity, term)
if len(args) == 0 { if len(args) == 0 {
return errors.Fatal("no snapshot ID specified, specify snapshot ID or use special ID 'latest'") return errors.Fatal("no snapshot ID specified, specify snapshot ID or use special ID 'latest'")
@@ -373,7 +373,7 @@ func runLs(ctx context.Context, opts LsOptions, gopts GlobalOptions, args []stri
return err return err
} }
bar := newIndexTerminalProgress(termPrinter) bar := ui.NewIndexCounter(termPrinter)
if err = repo.LoadIndex(ctx, bar); err != nil { if err = repo.LoadIndex(ctx, bar); err != nil {
return err return err
} }

View File

@@ -134,7 +134,7 @@ func applyMigrations(ctx context.Context, opts MigrateOptions, gopts GlobalOptio
} }
func runMigrate(ctx context.Context, opts MigrateOptions, gopts GlobalOptions, args []string, term ui.Terminal) error { func runMigrate(ctx context.Context, opts MigrateOptions, gopts GlobalOptions, args []string, term ui.Terminal) error {
printer := newTerminalProgressPrinter(false, gopts.verbosity, term) printer := ui.NewProgressPrinter(false, gopts.verbosity, term)
ctx, repo, unlock, err := openWithExclusiveLock(ctx, gopts, false, printer) ctx, repo, unlock, err := openWithExclusiveLock(ctx, gopts, false, printer)
if err != nil { if err != nil {

View File

@@ -114,7 +114,7 @@ func (opts *MountOptions) AddFlags(f *pflag.FlagSet) {
} }
func runMount(ctx context.Context, opts MountOptions, gopts GlobalOptions, args []string, term ui.Terminal) error { func runMount(ctx context.Context, opts MountOptions, gopts GlobalOptions, args []string, term ui.Terminal) error {
printer := newTerminalProgressPrinter(false, gopts.verbosity, term) printer := ui.NewProgressPrinter(false, gopts.verbosity, term)
if opts.TimeTemplate == "" { if opts.TimeTemplate == "" {
return errors.Fatal("time template string cannot be empty") return errors.Fatal("time template string cannot be empty")
@@ -146,7 +146,7 @@ func runMount(ctx context.Context, opts MountOptions, gopts GlobalOptions, args
} }
defer unlock() defer unlock()
bar := newIndexTerminalProgress(printer) bar := ui.NewIndexCounter(printer)
err = repo.LoadIndex(ctx, bar) err = repo.LoadIndex(ctx, bar)
if err != nil { if err != nil {
return err return err

View File

@@ -16,6 +16,7 @@ import (
"github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
rtest "github.com/restic/restic/internal/test" rtest "github.com/restic/restic/internal/test"
"github.com/restic/restic/internal/ui"
) )
const ( const (
@@ -128,7 +129,7 @@ func checkSnapshots(t testing.TB, gopts GlobalOptions, mountpoint string, snapsh
} }
err := withTermStatus(gopts, func(ctx context.Context, gopts GlobalOptions) error { err := withTermStatus(gopts, func(ctx context.Context, gopts GlobalOptions) error {
printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, gopts.term) printer := ui.NewProgressPrinter(gopts.JSON, gopts.verbosity, gopts.term)
_, repo, unlock, err := openWithReadLock(ctx, gopts, false, printer) _, repo, unlock, err := openWithReadLock(ctx, gopts, false, printer)
if err != nil { if err != nil {
return err return err

View File

@@ -166,7 +166,7 @@ func runPrune(ctx context.Context, opts PruneOptions, gopts GlobalOptions, term
return errors.Fatal("--no-lock is only applicable in combination with --dry-run for prune command") return errors.Fatal("--no-lock is only applicable in combination with --dry-run for prune command")
} }
printer := newTerminalProgressPrinter(false, gopts.verbosity, term) printer := ui.NewProgressPrinter(false, gopts.verbosity, term)
ctx, repo, unlock, err := openWithExclusiveLock(ctx, gopts, opts.DryRun && gopts.NoLock, printer) ctx, repo, unlock, err := openWithExclusiveLock(ctx, gopts, opts.DryRun && gopts.NoLock, printer)
if err != nil { if err != nil {
return err return err
@@ -190,7 +190,7 @@ func runPruneWithRepo(ctx context.Context, opts PruneOptions, repo *repository.R
} }
// loading the index before the snapshots is ok, as we use an exclusive lock here // loading the index before the snapshots is ok, as we use an exclusive lock here
bar := newIndexTerminalProgress(printer) bar := ui.NewIndexCounter(printer)
err := repo.LoadIndex(ctx, bar) err := repo.LoadIndex(ctx, bar)
if err != nil { if err != nil {
return err return err

View File

@@ -47,7 +47,7 @@ func runRecover(ctx context.Context, gopts GlobalOptions, term ui.Terminal) erro
return err return err
} }
printer := newTerminalProgressPrinter(false, gopts.verbosity, term) printer := ui.NewProgressPrinter(false, gopts.verbosity, term)
ctx, repo, unlock, err := openWithExclusiveLock(ctx, gopts, false, printer) ctx, repo, unlock, err := openWithExclusiveLock(ctx, gopts, false, printer)
if err != nil { if err != nil {
return err return err
@@ -66,7 +66,7 @@ func runRecover(ctx context.Context, gopts GlobalOptions, term ui.Terminal) erro
} }
printer.P("load index files\n") printer.P("load index files\n")
bar := newIndexTerminalProgress(printer) bar := ui.NewIndexCounter(printer)
if err = repo.LoadIndex(ctx, bar); err != nil { if err = repo.LoadIndex(ctx, bar); err != nil {
return err return err
} }

View File

@@ -69,7 +69,7 @@ func newRebuildIndexCommand(globalOptions *GlobalOptions) *cobra.Command {
} }
func runRebuildIndex(ctx context.Context, opts RepairIndexOptions, gopts GlobalOptions, term ui.Terminal) error { func runRebuildIndex(ctx context.Context, opts RepairIndexOptions, gopts GlobalOptions, term ui.Terminal) error {
printer := newTerminalProgressPrinter(false, gopts.verbosity, term) printer := ui.NewProgressPrinter(false, gopts.verbosity, term)
ctx, repo, unlock, err := openWithExclusiveLock(ctx, gopts, false, printer) ctx, repo, unlock, err := openWithExclusiveLock(ctx, gopts, false, printer)
if err != nil { if err != nil {

View File

@@ -51,7 +51,7 @@ func runRepairPacks(ctx context.Context, gopts GlobalOptions, term ui.Terminal,
return errors.Fatal("no ids specified") return errors.Fatal("no ids specified")
} }
printer := newTerminalProgressPrinter(false, gopts.verbosity, term) printer := ui.NewProgressPrinter(false, gopts.verbosity, term)
ctx, repo, unlock, err := openWithExclusiveLock(ctx, gopts, false, printer) ctx, repo, unlock, err := openWithExclusiveLock(ctx, gopts, false, printer)
if err != nil { if err != nil {
@@ -59,7 +59,7 @@ func runRepairPacks(ctx context.Context, gopts GlobalOptions, term ui.Terminal,
} }
defer unlock() defer unlock()
bar := newIndexTerminalProgress(printer) bar := ui.NewIndexCounter(printer)
err = repo.LoadIndex(ctx, bar) err = repo.LoadIndex(ctx, bar)
if err != nil { if err != nil {
return errors.Fatalf("%s", err) return errors.Fatalf("%s", err)

View File

@@ -74,7 +74,7 @@ func (opts *RepairOptions) AddFlags(f *pflag.FlagSet) {
} }
func runRepairSnapshots(ctx context.Context, gopts GlobalOptions, opts RepairOptions, args []string, term ui.Terminal) error { func runRepairSnapshots(ctx context.Context, gopts GlobalOptions, opts RepairOptions, args []string, term ui.Terminal) error {
printer := newTerminalProgressPrinter(false, gopts.verbosity, term) printer := ui.NewProgressPrinter(false, gopts.verbosity, term)
ctx, repo, unlock, err := openWithExclusiveLock(ctx, gopts, opts.DryRun, printer) ctx, repo, unlock, err := openWithExclusiveLock(ctx, gopts, opts.DryRun, printer)
if err != nil { if err != nil {
@@ -87,7 +87,7 @@ func runRepairSnapshots(ctx context.Context, gopts GlobalOptions, opts RepairOpt
return err return err
} }
bar := newIndexTerminalProgress(printer) bar := ui.NewIndexCounter(printer)
if err := repo.LoadIndex(ctx, bar); err != nil { if err := repo.LoadIndex(ctx, bar); err != nil {
return err return err
} }

View File

@@ -89,7 +89,7 @@ func (opts *RestoreOptions) AddFlags(f *pflag.FlagSet) {
func runRestore(ctx context.Context, opts RestoreOptions, gopts GlobalOptions, func runRestore(ctx context.Context, opts RestoreOptions, gopts GlobalOptions,
term ui.Terminal, args []string) error { term ui.Terminal, args []string) error {
msg := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, term) msg := ui.NewProgressPrinter(gopts.JSON, gopts.verbosity, term)
excludePatternFns, err := opts.ExcludePatternOptions.CollectPatterns(msg.E) excludePatternFns, err := opts.ExcludePatternOptions.CollectPatterns(msg.E)
if err != nil { if err != nil {
return err return err
@@ -145,7 +145,7 @@ func runRestore(ctx context.Context, opts RestoreOptions, gopts GlobalOptions,
return errors.Fatalf("failed to find snapshot: %v", err) return errors.Fatalf("failed to find snapshot: %v", err)
} }
bar := newIndexTerminalProgress(msg) bar := ui.NewIndexCounter(msg)
err = repo.LoadIndex(ctx, bar) err = repo.LoadIndex(ctx, bar)
if err != nil { if err != nil {
return err return err
@@ -163,7 +163,7 @@ func runRestore(ctx context.Context, opts RestoreOptions, gopts GlobalOptions,
printer = restoreui.NewTextProgress(term, gopts.verbosity) printer = restoreui.NewTextProgress(term, gopts.verbosity)
} }
progress := restoreui.NewProgress(printer, calculateProgressInterval(!gopts.Quiet, gopts.JSON, term.CanUpdateStatus())) progress := restoreui.NewProgress(printer, ui.CalculateProgressInterval(!gopts.Quiet, gopts.JSON, term.CanUpdateStatus()))
res := restorer.NewRestorer(repo, sn, restorer.Options{ res := restorer.NewRestorer(repo, sn, restorer.Options{
DryRun: opts.DryRun, DryRun: opts.DryRun,
Sparse: opts.Sparse, Sparse: opts.Sparse,

View File

@@ -294,7 +294,7 @@ func runRewrite(ctx context.Context, opts RewriteOptions, gopts GlobalOptions, a
return errors.Fatal("Nothing to do: no excludes provided and no new metadata provided") return errors.Fatal("Nothing to do: no excludes provided and no new metadata provided")
} }
printer := newTerminalProgressPrinter(false, gopts.verbosity, term) printer := ui.NewProgressPrinter(false, gopts.verbosity, term)
var ( var (
repo *repository.Repository repo *repository.Repository
@@ -318,7 +318,7 @@ func runRewrite(ctx context.Context, opts RewriteOptions, gopts GlobalOptions, a
return err return err
} }
bar := newIndexTerminalProgress(printer) bar := ui.NewIndexCounter(printer)
if err = repo.LoadIndex(ctx, bar); err != nil { if err = repo.LoadIndex(ctx, bar); err != nil {
return err return err
} }

View File

@@ -42,7 +42,7 @@ func getSnapshot(t testing.TB, snapshotID restic.ID, env *testEnvironment) *rest
var snapshots []*restic.Snapshot var snapshots []*restic.Snapshot
err := withTermStatus(env.gopts, func(ctx context.Context, gopts GlobalOptions) error { err := withTermStatus(env.gopts, func(ctx context.Context, gopts GlobalOptions) error {
printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, gopts.term) printer := ui.NewProgressPrinter(gopts.JSON, gopts.verbosity, gopts.term)
ctx, repo, unlock, err := openWithReadLock(ctx, gopts, false, printer) ctx, repo, unlock, err := openWithReadLock(ctx, gopts, false, printer)
rtest.OK(t, err) rtest.OK(t, err)
defer unlock() defer unlock()
@@ -118,7 +118,7 @@ func testRewriteMetadata(t *testing.T, metadata snapshotMetadataArgs) {
var snapshots []*restic.Snapshot var snapshots []*restic.Snapshot
err := withTermStatus(env.gopts, func(ctx context.Context, gopts GlobalOptions) error { err := withTermStatus(env.gopts, func(ctx context.Context, gopts GlobalOptions) error {
printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, gopts.term) printer := ui.NewProgressPrinter(gopts.JSON, gopts.verbosity, gopts.term)
ctx, repo, unlock, err := openWithReadLock(ctx, gopts, false, printer) ctx, repo, unlock, err := openWithReadLock(ctx, gopts, false, printer)
rtest.OK(t, err) rtest.OK(t, err)
defer unlock() defer unlock()
@@ -166,7 +166,7 @@ func TestRewriteSnaphotSummary(t *testing.T) {
// replace snapshot by one without a summary // replace snapshot by one without a summary
var oldSummary *restic.SnapshotSummary var oldSummary *restic.SnapshotSummary
err := withTermStatus(env.gopts, func(ctx context.Context, gopts GlobalOptions) error { err := withTermStatus(env.gopts, func(ctx context.Context, gopts GlobalOptions) error {
printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, gopts.term) printer := ui.NewProgressPrinter(gopts.JSON, gopts.verbosity, gopts.term)
_, repo, unlock, err := openWithExclusiveLock(ctx, gopts, false, printer) _, repo, unlock, err := openWithExclusiveLock(ctx, gopts, false, printer)
rtest.OK(t, err) rtest.OK(t, err)
defer unlock() defer unlock()

View File

@@ -86,7 +86,7 @@ func runSelfUpdate(ctx context.Context, opts SelfUpdateOptions, gopts GlobalOpti
} }
} }
printer := newTerminalProgressPrinter(false, gopts.verbosity, term) printer := ui.NewProgressPrinter(false, gopts.verbosity, term)
printer.P("writing restic to %v", opts.Output) printer.P("writing restic to %v", opts.Output)
v, err := selfupdate.DownloadLatestStableRelease(ctx, opts.Output, version, printer.P) v, err := selfupdate.DownloadLatestStableRelease(ctx, opts.Output, version, printer.P)

View File

@@ -67,7 +67,7 @@ func (opts *SnapshotOptions) AddFlags(f *pflag.FlagSet) {
} }
func runSnapshots(ctx context.Context, opts SnapshotOptions, gopts GlobalOptions, args []string, term ui.Terminal) error { func runSnapshots(ctx context.Context, opts SnapshotOptions, gopts GlobalOptions, args []string, term ui.Terminal) error {
printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, term) printer := ui.NewProgressPrinter(gopts.JSON, gopts.verbosity, term)
ctx, repo, unlock, err := openWithReadLock(ctx, gopts, gopts.NoLock, printer) ctx, repo, unlock, err := openWithReadLock(ctx, gopts, gopts.NoLock, printer)
if err != nil { if err != nil {
return err return err

View File

@@ -99,7 +99,7 @@ func runStats(ctx context.Context, opts StatsOptions, gopts GlobalOptions, args
return err return err
} }
printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, term) printer := ui.NewProgressPrinter(gopts.JSON, gopts.verbosity, term)
ctx, repo, unlock, err := openWithReadLock(ctx, gopts, gopts.NoLock, printer) ctx, repo, unlock, err := openWithReadLock(ctx, gopts, gopts.NoLock, printer)
if err != nil { if err != nil {
@@ -111,7 +111,7 @@ func runStats(ctx context.Context, opts StatsOptions, gopts GlobalOptions, args
if err != nil { if err != nil {
return err return err
} }
bar := newIndexTerminalProgress(printer) bar := ui.NewIndexCounter(printer)
if err = repo.LoadIndex(ctx, bar); err != nil { if err = repo.LoadIndex(ctx, bar); err != nil {
return err return err
} }

View File

@@ -117,7 +117,7 @@ func changeTags(ctx context.Context, repo *repository.Repository, sn *restic.Sna
} }
func runTag(ctx context.Context, opts TagOptions, gopts GlobalOptions, term ui.Terminal, args []string) error { func runTag(ctx context.Context, opts TagOptions, gopts GlobalOptions, term ui.Terminal, args []string) error {
printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, term) printer := ui.NewProgressPrinter(gopts.JSON, gopts.verbosity, term)
if len(opts.SetTags) == 0 && len(opts.AddTags) == 0 && len(opts.RemoveTags) == 0 { if len(opts.SetTags) == 0 && len(opts.AddTags) == 0 && len(opts.RemoveTags) == 0 {
return errors.Fatal("nothing to do!") return errors.Fatal("nothing to do!")

View File

@@ -44,7 +44,7 @@ func (opts *UnlockOptions) AddFlags(f *pflag.FlagSet) {
} }
func runUnlock(ctx context.Context, opts UnlockOptions, gopts GlobalOptions, term ui.Terminal) error { func runUnlock(ctx context.Context, opts UnlockOptions, gopts GlobalOptions, term ui.Terminal) error {
printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, term) printer := ui.NewProgressPrinter(gopts.JSON, gopts.verbosity, term)
repo, err := OpenRepository(ctx, gopts, printer) repo, err := OpenRepository(ctx, gopts, printer)
if err != nil { if err != nil {
return err return err

View File

@@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"runtime" "runtime"
"github.com/restic/restic/internal/ui"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@@ -23,7 +24,7 @@ Exit status is 1 if there was any error.
`, `,
DisableAutoGenTag: true, DisableAutoGenTag: true,
Run: func(_ *cobra.Command, _ []string) { Run: func(_ *cobra.Command, _ []string) {
printer := newTerminalProgressPrinter(globalOptions.JSON, globalOptions.verbosity, globalOptions.term) printer := ui.NewProgressPrinter(globalOptions.JSON, globalOptions.verbosity, globalOptions.term)
if globalOptions.JSON { if globalOptions.JSON {
type jsonVersion struct { type jsonVersion struct {

View File

@@ -20,6 +20,7 @@ import (
"github.com/restic/restic/internal/repository" "github.com/restic/restic/internal/repository"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
rtest "github.com/restic/restic/internal/test" rtest "github.com/restic/restic/internal/test"
"github.com/restic/restic/internal/ui"
"github.com/restic/restic/internal/ui/termstatus" "github.com/restic/restic/internal/ui/termstatus"
) )
@@ -245,7 +246,7 @@ func testSetupBackupData(t testing.TB, env *testEnvironment) string {
func listPacks(gopts GlobalOptions, t *testing.T) restic.IDSet { func listPacks(gopts GlobalOptions, t *testing.T) restic.IDSet {
var packs restic.IDSet var packs restic.IDSet
err := withTermStatus(gopts, func(ctx context.Context, gopts GlobalOptions) error { err := withTermStatus(gopts, func(ctx context.Context, gopts GlobalOptions) error {
printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, gopts.term) printer := ui.NewProgressPrinter(gopts.JSON, gopts.verbosity, gopts.term)
ctx, r, unlock, err := openWithReadLock(ctx, gopts, false, printer) ctx, r, unlock, err := openWithReadLock(ctx, gopts, false, printer)
rtest.OK(t, err) rtest.OK(t, err)
defer unlock() defer unlock()
@@ -264,7 +265,7 @@ func listPacks(gopts GlobalOptions, t *testing.T) restic.IDSet {
func listTreePacks(gopts GlobalOptions, t *testing.T) restic.IDSet { func listTreePacks(gopts GlobalOptions, t *testing.T) restic.IDSet {
var treePacks restic.IDSet var treePacks restic.IDSet
err := withTermStatus(gopts, func(ctx context.Context, gopts GlobalOptions) error { err := withTermStatus(gopts, func(ctx context.Context, gopts GlobalOptions) error {
printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, gopts.term) printer := ui.NewProgressPrinter(gopts.JSON, gopts.verbosity, gopts.term)
ctx, r, unlock, err := openWithReadLock(ctx, gopts, false, printer) ctx, r, unlock, err := openWithReadLock(ctx, gopts, false, printer)
rtest.OK(t, err) rtest.OK(t, err)
defer unlock() defer unlock()
@@ -295,7 +296,7 @@ func captureBackend(gopts *GlobalOptions) func() backend.Backend {
func removePacks(gopts GlobalOptions, t testing.TB, remove restic.IDSet) { func removePacks(gopts GlobalOptions, t testing.TB, remove restic.IDSet) {
be := captureBackend(&gopts) be := captureBackend(&gopts)
err := withTermStatus(gopts, func(ctx context.Context, gopts GlobalOptions) error { err := withTermStatus(gopts, func(ctx context.Context, gopts GlobalOptions) error {
printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, gopts.term) printer := ui.NewProgressPrinter(gopts.JSON, gopts.verbosity, gopts.term)
ctx, _, unlock, err := openWithExclusiveLock(ctx, gopts, false, printer) ctx, _, unlock, err := openWithExclusiveLock(ctx, gopts, false, printer)
rtest.OK(t, err) rtest.OK(t, err)
defer unlock() defer unlock()
@@ -311,7 +312,7 @@ func removePacks(gopts GlobalOptions, t testing.TB, remove restic.IDSet) {
func removePacksExcept(gopts GlobalOptions, t testing.TB, keep restic.IDSet, removeTreePacks bool) { func removePacksExcept(gopts GlobalOptions, t testing.TB, keep restic.IDSet, removeTreePacks bool) {
be := captureBackend(&gopts) be := captureBackend(&gopts)
err := withTermStatus(gopts, func(ctx context.Context, gopts GlobalOptions) error { err := withTermStatus(gopts, func(ctx context.Context, gopts GlobalOptions) error {
printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, gopts.term) printer := ui.NewProgressPrinter(gopts.JSON, gopts.verbosity, gopts.term)
ctx, r, unlock, err := openWithExclusiveLock(ctx, gopts, false, printer) ctx, r, unlock, err := openWithExclusiveLock(ctx, gopts, false, printer)
rtest.OK(t, err) rtest.OK(t, err)
defer unlock() defer unlock()
@@ -372,7 +373,7 @@ func lastSnapshot(old, new map[string]struct{}) (map[string]struct{}, string) {
func testLoadSnapshot(t testing.TB, gopts GlobalOptions, id restic.ID) *restic.Snapshot { func testLoadSnapshot(t testing.TB, gopts GlobalOptions, id restic.ID) *restic.Snapshot {
var snapshot *restic.Snapshot var snapshot *restic.Snapshot
err := withTermStatus(gopts, func(ctx context.Context, gopts GlobalOptions) error { err := withTermStatus(gopts, func(ctx context.Context, gopts GlobalOptions) error {
printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, gopts.term) printer := ui.NewProgressPrinter(gopts.JSON, gopts.verbosity, gopts.term)
_, repo, unlock, err := openWithReadLock(ctx, gopts, false, printer) _, repo, unlock, err := openWithReadLock(ctx, gopts, false, printer)
rtest.OK(t, err) rtest.OK(t, err)
defer unlock() defer unlock()

View File

@@ -12,6 +12,7 @@ import (
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
rtest "github.com/restic/restic/internal/test" rtest "github.com/restic/restic/internal/test"
"github.com/restic/restic/internal/ui"
) )
func TestCheckRestoreNoLock(t *testing.T) { func TestCheckRestoreNoLock(t *testing.T) {
@@ -162,7 +163,7 @@ func TestFindListOnce(t *testing.T) {
var snapshotIDs restic.IDSet var snapshotIDs restic.IDSet
rtest.OK(t, withTermStatus(env.gopts, func(ctx context.Context, gopts GlobalOptions) error { rtest.OK(t, withTermStatus(env.gopts, func(ctx context.Context, gopts GlobalOptions) error {
printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, gopts.term) printer := ui.NewProgressPrinter(gopts.JSON, gopts.verbosity, gopts.term)
ctx, repo, unlock, err := openWithReadLock(ctx, gopts, false, printer) ctx, repo, unlock, err := openWithReadLock(ctx, gopts, false, printer)
rtest.OK(t, err) rtest.OK(t, err)
defer unlock() defer unlock()

View File

@@ -1,83 +0,0 @@
package main
import (
"fmt"
"os"
"strconv"
"time"
"github.com/restic/restic/internal/ui"
"github.com/restic/restic/internal/ui/progress"
)
// calculateProgressInterval returns the interval configured via RESTIC_PROGRESS_FPS
// or if unset returns an interval for 60fps on interactive terminals and 0 (=disabled)
// for non-interactive terminals or when run using the --quiet flag
func calculateProgressInterval(show bool, json bool, canUpdateStatus bool) time.Duration {
interval := time.Second / 60
fps, err := strconv.ParseFloat(os.Getenv("RESTIC_PROGRESS_FPS"), 64)
if err == nil && fps > 0 {
if fps > 60 {
fps = 60
}
interval = time.Duration(float64(time.Second) / fps)
} else if !json && !canUpdateStatus || !show {
interval = 0
}
return interval
}
// newTerminalProgressMax returns a progress.Counter that prints to terminal if provided.
func newTerminalProgressMax(show bool, max uint64, description string, term ui.Terminal) *progress.Counter {
if !show {
return nil
}
interval := calculateProgressInterval(show, false, term.CanUpdateStatus())
return progress.NewCounter(interval, max, func(v uint64, max uint64, d time.Duration, final bool) {
var status string
if max == 0 {
status = fmt.Sprintf("[%s] %d %s",
ui.FormatDuration(d), v, description)
} else {
status = fmt.Sprintf("[%s] %s %d / %d %s",
ui.FormatDuration(d), ui.FormatPercent(v, max), v, max, description)
}
if final {
term.SetStatus(nil)
term.Print(status)
} else {
term.SetStatus([]string{status})
}
})
}
type terminalProgressPrinter struct {
term ui.Terminal
ui.Message
show bool
}
func (t *terminalProgressPrinter) NewCounter(description string) *progress.Counter {
return newTerminalProgressMax(t.show, 0, description, t.term)
}
func (t *terminalProgressPrinter) NewCounterTerminalOnly(description string) *progress.Counter {
return newTerminalProgressMax(t.show && t.term.OutputIsTerminal(), 0, description, t.term)
}
func newTerminalProgressPrinter(json bool, verbosity uint, term ui.Terminal) progress.Printer {
if json {
verbosity = 0
}
return &terminalProgressPrinter{
term: term,
Message: *ui.NewMessage(term, verbosity),
show: verbosity > 0,
}
}
func newIndexTerminalProgress(printer progress.Printer) *progress.Counter {
return printer.NewCounterTerminalOnly("index files loaded")
}

82
internal/ui/progress.go Normal file
View File

@@ -0,0 +1,82 @@
package ui
import (
"fmt"
"os"
"strconv"
"time"
"github.com/restic/restic/internal/ui/progress"
)
// CalculateProgressInterval returns the interval configured via RESTIC_PROGRESS_FPS
// or if unset returns an interval for 60fps on interactive terminals and 0 (=disabled)
// for non-interactive terminals or when run using the --quiet flag
func CalculateProgressInterval(show bool, json bool, canUpdateStatus bool) time.Duration {
interval := time.Second / 60
fps, err := strconv.ParseFloat(os.Getenv("RESTIC_PROGRESS_FPS"), 64)
if err == nil && fps > 0 {
if fps > 60 {
fps = 60
}
interval = time.Duration(float64(time.Second) / fps)
} else if !json && !canUpdateStatus || !show {
interval = 0
}
return interval
}
// newProgressMax returns a progress.Counter that prints to terminal if provided.
func newProgressMax(show bool, max uint64, description string, term Terminal) *progress.Counter {
if !show {
return nil
}
interval := CalculateProgressInterval(show, false, term.CanUpdateStatus())
return progress.NewCounter(interval, max, func(v uint64, max uint64, d time.Duration, final bool) {
var status string
if max == 0 {
status = fmt.Sprintf("[%s] %d %s",
FormatDuration(d), v, description)
} else {
status = fmt.Sprintf("[%s] %s %d / %d %s",
FormatDuration(d), FormatPercent(v, max), v, max, description)
}
if final {
term.SetStatus(nil)
term.Print(status)
} else {
term.SetStatus([]string{status})
}
})
}
type progressPrinter struct {
term Terminal
Message
show bool
}
func (t *progressPrinter) NewCounter(description string) *progress.Counter {
return newProgressMax(t.show, 0, description, t.term)
}
func (t *progressPrinter) NewCounterTerminalOnly(description string) *progress.Counter {
return newProgressMax(t.show && t.term.OutputIsTerminal(), 0, description, t.term)
}
func NewProgressPrinter(json bool, verbosity uint, term Terminal) progress.Printer {
if json {
verbosity = 0
}
return &progressPrinter{
term: term,
Message: *NewMessage(term, verbosity),
show: verbosity > 0,
}
}
func NewIndexCounter(printer progress.Printer) *progress.Counter {
return printer.NewCounterTerminalOnly("index files loaded")
}