From c15b4bceaeeffd6a2eeb15106d7ada98a98fa436 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Wed, 28 Dec 2022 21:55:02 +0100 Subject: [PATCH] backup: extract StdioWrapper from ProgressPrinters The StdioWrapper is not used at all by the ProgressPrinters. It is called a bit earlier than previously. However, as the password prompt directly accessed stdin/stdout this doesn't cause problems. --- cmd/restic/cmd_backup.go | 16 +++++++++------- internal/ui/backup/json.go | 8 +++----- internal/ui/backup/progress.go | 5 ----- internal/ui/backup/progress_test.go | 4 ---- internal/ui/backup/text.go | 6 ++---- 5 files changed, 14 insertions(+), 25 deletions(-) diff --git a/cmd/restic/cmd_backup.go b/cmd/restic/cmd_backup.go index d29f3b371..683ce9268 100644 --- a/cmd/restic/cmd_backup.go +++ b/cmd/restic/cmd_backup.go @@ -25,6 +25,7 @@ import ( "github.com/restic/restic/internal/repository" "github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/textfile" + "github.com/restic/restic/internal/ui" "github.com/restic/restic/internal/ui/backup" "github.com/restic/restic/internal/ui/termstatus" ) @@ -71,6 +72,14 @@ Exit status is 3 if some source data could not be read (incomplete snapshot crea term.Run(cancelCtx) }() + // use the terminal for stdout/stderr + prevStdout, prevStderr := globalOptions.stdout, globalOptions.stderr + defer func() { + globalOptions.stdout, globalOptions.stderr = prevStdout, prevStderr + }() + stdioWrapper := ui.NewStdioWrapper(term) + globalOptions.stdout, globalOptions.stderr = stdioWrapper.Stdout(), stdioWrapper.Stderr() + return runBackup(ctx, backupOptions, globalOptions, term, args) }, } @@ -479,13 +488,6 @@ func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, ter repo.SetDryRun() } - // use the terminal for stdout/stderr - prevStdout, prevStderr := gopts.stdout, gopts.stderr - defer func() { - gopts.stdout, gopts.stderr = prevStdout, prevStderr - }() - gopts.stdout, gopts.stderr = progressPrinter.Stdout(), progressPrinter.Stderr() - wg, wgCtx := errgroup.WithContext(ctx) cancelCtx, cancel := context.WithCancel(wgCtx) defer cancel() diff --git a/internal/ui/backup/json.go b/internal/ui/backup/json.go index ef3d568fe..85076b3bb 100644 --- a/internal/ui/backup/json.go +++ b/internal/ui/backup/json.go @@ -15,7 +15,6 @@ import ( // JSONProgress reports progress for the `backup` command in JSON. type JSONProgress struct { *ui.Message - *ui.StdioWrapper term *termstatus.Terminal v uint @@ -27,10 +26,9 @@ var _ ProgressPrinter = &JSONProgress{} // NewJSONProgress returns a new backup progress reporter. func NewJSONProgress(term *termstatus.Terminal, verbosity uint) *JSONProgress { return &JSONProgress{ - Message: ui.NewMessage(term, verbosity), - StdioWrapper: ui.NewStdioWrapper(term), - term: term, - v: verbosity, + Message: ui.NewMessage(term, verbosity), + term: term, + v: verbosity, } } diff --git a/internal/ui/backup/progress.go b/internal/ui/backup/progress.go index 10e140927..746fe7a49 100644 --- a/internal/ui/backup/progress.go +++ b/internal/ui/backup/progress.go @@ -2,7 +2,6 @@ package backup import ( "context" - "io" "sync" "time" @@ -22,10 +21,6 @@ type ProgressPrinter interface { Finish(snapshotID restic.ID, start time.Time, summary *Summary, dryRun bool) Reset() - // ui.StdioWrapper - Stdout() io.WriteCloser - Stderr() io.WriteCloser - P(msg string, args ...interface{}) V(msg string, args ...interface{}) } diff --git a/internal/ui/backup/progress_test.go b/internal/ui/backup/progress_test.go index 248b24034..e0dc093d2 100644 --- a/internal/ui/backup/progress_test.go +++ b/internal/ui/backup/progress_test.go @@ -2,7 +2,6 @@ package backup import ( "context" - "io" "sync" "testing" "time" @@ -45,9 +44,6 @@ func (p *mockPrinter) Finish(id restic.ID, _ time.Time, summary *Summary, dryRun func (p *mockPrinter) Reset() {} -func (p *mockPrinter) Stdout() io.WriteCloser { return nil } -func (p *mockPrinter) Stderr() io.WriteCloser { return nil } - func (p *mockPrinter) P(msg string, args ...interface{}) {} func (p *mockPrinter) V(msg string, args ...interface{}) {} diff --git a/internal/ui/backup/text.go b/internal/ui/backup/text.go index dd3ea5b65..0c5f897dd 100644 --- a/internal/ui/backup/text.go +++ b/internal/ui/backup/text.go @@ -14,7 +14,6 @@ import ( // TextProgress reports progress for the `backup` command. type TextProgress struct { *ui.Message - *ui.StdioWrapper term *termstatus.Terminal } @@ -25,9 +24,8 @@ var _ ProgressPrinter = &TextProgress{} // NewTextProgress returns a new backup progress reporter. func NewTextProgress(term *termstatus.Terminal, verbosity uint) *TextProgress { return &TextProgress{ - Message: ui.NewMessage(term, verbosity), - StdioWrapper: ui.NewStdioWrapper(term), - term: term, + Message: ui.NewMessage(term, verbosity), + term: term, } }