2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-22 12:55:18 +00:00

Merge pull request #4111 from MichaelEischer/extract-stdio-wrapper

backup: extract StdioWrapper from ProgressPrinters
This commit is contained in:
Michael Eischer 2023-01-14 01:12:23 +01:00 committed by GitHub
commit 52682b1c7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 14 additions and 25 deletions

View File

@ -25,6 +25,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"
"github.com/restic/restic/internal/textfile" "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/backup"
"github.com/restic/restic/internal/ui/termstatus" "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) 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) return runBackup(ctx, backupOptions, globalOptions, term, args)
}, },
} }
@ -479,13 +488,6 @@ func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, ter
repo.SetDryRun() 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) wg, wgCtx := errgroup.WithContext(ctx)
cancelCtx, cancel := context.WithCancel(wgCtx) cancelCtx, cancel := context.WithCancel(wgCtx)
defer cancel() defer cancel()

View File

@ -15,7 +15,6 @@ import (
// JSONProgress reports progress for the `backup` command in JSON. // JSONProgress reports progress for the `backup` command in JSON.
type JSONProgress struct { type JSONProgress struct {
*ui.Message *ui.Message
*ui.StdioWrapper
term *termstatus.Terminal term *termstatus.Terminal
v uint v uint
@ -27,10 +26,9 @@ var _ ProgressPrinter = &JSONProgress{}
// NewJSONProgress returns a new backup progress reporter. // NewJSONProgress returns a new backup progress reporter.
func NewJSONProgress(term *termstatus.Terminal, verbosity uint) *JSONProgress { func NewJSONProgress(term *termstatus.Terminal, verbosity uint) *JSONProgress {
return &JSONProgress{ return &JSONProgress{
Message: ui.NewMessage(term, verbosity), Message: ui.NewMessage(term, verbosity),
StdioWrapper: ui.NewStdioWrapper(term), term: term,
term: term, v: verbosity,
v: verbosity,
} }
} }

View File

@ -2,7 +2,6 @@ package backup
import ( import (
"context" "context"
"io"
"sync" "sync"
"time" "time"
@ -22,10 +21,6 @@ type ProgressPrinter interface {
Finish(snapshotID restic.ID, start time.Time, summary *Summary, dryRun bool) Finish(snapshotID restic.ID, start time.Time, summary *Summary, dryRun bool)
Reset() Reset()
// ui.StdioWrapper
Stdout() io.WriteCloser
Stderr() io.WriteCloser
P(msg string, args ...interface{}) P(msg string, args ...interface{})
V(msg string, args ...interface{}) V(msg string, args ...interface{})
} }

View File

@ -2,7 +2,6 @@ package backup
import ( import (
"context" "context"
"io"
"sync" "sync"
"testing" "testing"
"time" "time"
@ -45,9 +44,6 @@ func (p *mockPrinter) Finish(id restic.ID, _ time.Time, summary *Summary, dryRun
func (p *mockPrinter) Reset() {} 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) P(msg string, args ...interface{}) {}
func (p *mockPrinter) V(msg string, args ...interface{}) {} func (p *mockPrinter) V(msg string, args ...interface{}) {}

View File

@ -14,7 +14,6 @@ import (
// TextProgress reports progress for the `backup` command. // TextProgress reports progress for the `backup` command.
type TextProgress struct { type TextProgress struct {
*ui.Message *ui.Message
*ui.StdioWrapper
term *termstatus.Terminal term *termstatus.Terminal
} }
@ -25,9 +24,8 @@ var _ ProgressPrinter = &TextProgress{}
// NewTextProgress returns a new backup progress reporter. // NewTextProgress returns a new backup progress reporter.
func NewTextProgress(term *termstatus.Terminal, verbosity uint) *TextProgress { func NewTextProgress(term *termstatus.Terminal, verbosity uint) *TextProgress {
return &TextProgress{ return &TextProgress{
Message: ui.NewMessage(term, verbosity), Message: ui.NewMessage(term, verbosity),
StdioWrapper: ui.NewStdioWrapper(term), term: term,
term: term,
} }
} }