mirror of
https://github.com/restic/restic.git
synced 2025-01-03 13:45:20 +00:00
restore: add basic json progress
This commit is contained in:
parent
a9aff885d6
commit
d54176ce5d
6 changed files with 104 additions and 27 deletions
|
@ -175,11 +175,14 @@ func runRestore(ctx context.Context, opts RestoreOptions, gopts GlobalOptions,
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var progress *restoreui.Progress
|
var printer restoreui.ProgressPrinter
|
||||||
if !gopts.Quiet && !gopts.JSON {
|
if gopts.JSON {
|
||||||
progress = restoreui.NewProgress(restoreui.NewTextPrinter(term), calculateProgressInterval(!gopts.Quiet, gopts.JSON))
|
printer = restoreui.NewJSONProgress(term)
|
||||||
|
} else {
|
||||||
|
printer = restoreui.NewTextProgress(term)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
progress := restoreui.NewProgress(printer, calculateProgressInterval(!gopts.Quiet, gopts.JSON))
|
||||||
res := restorer.NewRestorer(repo, sn, opts.Sparse, progress)
|
res := restorer.NewRestorer(repo, sn, opts.Sparse, progress)
|
||||||
|
|
||||||
totalErrors := 0
|
totalErrors := 0
|
||||||
|
@ -237,23 +240,25 @@ func runRestore(ctx context.Context, opts RestoreOptions, gopts GlobalOptions,
|
||||||
res.SelectFilter = selectIncludeFilter
|
res.SelectFilter = selectIncludeFilter
|
||||||
}
|
}
|
||||||
|
|
||||||
Verbosef("restoring %s to %s\n", res.Snapshot(), opts.Target)
|
if !gopts.JSON {
|
||||||
|
Verbosef("restoring %s to %s\n", res.Snapshot(), opts.Target)
|
||||||
|
}
|
||||||
|
|
||||||
err = res.RestoreTo(ctx, opts.Target)
|
err = res.RestoreTo(ctx, opts.Target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if progress != nil {
|
progress.Finish()
|
||||||
progress.Finish()
|
|
||||||
}
|
|
||||||
|
|
||||||
if totalErrors > 0 {
|
if totalErrors > 0 {
|
||||||
return errors.Fatalf("There were %d errors\n", totalErrors)
|
return errors.Fatalf("There were %d errors\n", totalErrors)
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.Verify {
|
if opts.Verify {
|
||||||
Verbosef("verifying files in %s\n", opts.Target)
|
if !gopts.JSON {
|
||||||
|
Verbosef("verifying files in %s\n", opts.Target)
|
||||||
|
}
|
||||||
var count int
|
var count int
|
||||||
t0 := time.Now()
|
t0 := time.Now()
|
||||||
count, err = res.VerifyFiles(ctx, opts.Target)
|
count, err = res.VerifyFiles(ctx, opts.Target)
|
||||||
|
@ -263,8 +268,11 @@ func runRestore(ctx context.Context, opts RestoreOptions, gopts GlobalOptions,
|
||||||
if totalErrors > 0 {
|
if totalErrors > 0 {
|
||||||
return errors.Fatalf("There were %d errors\n", totalErrors)
|
return errors.Fatalf("There were %d errors\n", totalErrors)
|
||||||
}
|
}
|
||||||
Verbosef("finished verifying %d files in %s (took %s)\n", count, opts.Target,
|
|
||||||
time.Since(t0).Round(time.Millisecond))
|
if !gopts.JSON {
|
||||||
|
Verbosef("finished verifying %d files in %s (took %s)\n", count, opts.Target,
|
||||||
|
time.Since(t0).Round(time.Millisecond))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
package backup
|
package backup
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"sort"
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -32,21 +30,12 @@ func NewJSONProgress(term *termstatus.Terminal, verbosity uint) *JSONProgress {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func toJSONString(status interface{}) string {
|
|
||||||
buf := new(bytes.Buffer)
|
|
||||||
err := json.NewEncoder(buf).Encode(status)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return buf.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *JSONProgress) print(status interface{}) {
|
func (b *JSONProgress) print(status interface{}) {
|
||||||
b.term.Print(toJSONString(status))
|
b.term.Print(ui.ToJSONString(status))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *JSONProgress) error(status interface{}) {
|
func (b *JSONProgress) error(status interface{}) {
|
||||||
b.term.Error(toJSONString(status))
|
b.term.Error(ui.ToJSONString(status))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update updates the status lines.
|
// Update updates the status lines.
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package ui
|
package ui
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -53,3 +55,12 @@ func FormatSeconds(sec uint64) string {
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("%d:%02d", min, sec)
|
return fmt.Sprintf("%d:%02d", min, sec)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ToJSONString(status interface{}) string {
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
err := json.NewEncoder(buf).Encode(status)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
69
internal/ui/restore/json.go
Normal file
69
internal/ui/restore/json.go
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
package restore
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/restic/restic/internal/ui"
|
||||||
|
)
|
||||||
|
|
||||||
|
type jsonPrinter struct {
|
||||||
|
terminal term
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewJSONProgress(terminal term) ProgressPrinter {
|
||||||
|
return &jsonPrinter{
|
||||||
|
terminal: terminal,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *jsonPrinter) print(status interface{}) {
|
||||||
|
t.terminal.Print(ui.ToJSONString(status))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *jsonPrinter) Update(filesFinished, filesTotal, allBytesWritten, allBytesTotal uint64, duration time.Duration) {
|
||||||
|
status := statusUpdate{
|
||||||
|
MessageType: "status",
|
||||||
|
SecondsElapsed: uint64(duration / time.Second),
|
||||||
|
TotalFiles: filesTotal,
|
||||||
|
FilesDone: filesFinished,
|
||||||
|
TotalBytes: allBytesTotal,
|
||||||
|
BytesDone: allBytesWritten,
|
||||||
|
}
|
||||||
|
|
||||||
|
if allBytesTotal > 0 {
|
||||||
|
status.PercentDone = float64(allBytesWritten) / float64(allBytesTotal)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.print(status)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *jsonPrinter) Finish(filesFinished, filesTotal, allBytesWritten, allBytesTotal uint64, duration time.Duration) {
|
||||||
|
status := summaryOutput{
|
||||||
|
MessageType: "summary",
|
||||||
|
SecondsElapsed: uint64(duration / time.Second),
|
||||||
|
TotalFiles: filesTotal,
|
||||||
|
FilesDone: filesFinished,
|
||||||
|
TotalBytes: allBytesTotal,
|
||||||
|
BytesDone: allBytesWritten,
|
||||||
|
}
|
||||||
|
t.print(status)
|
||||||
|
}
|
||||||
|
|
||||||
|
type statusUpdate struct {
|
||||||
|
MessageType string `json:"message_type"` // "status"
|
||||||
|
SecondsElapsed uint64 `json:"seconds_elapsed,omitempty"`
|
||||||
|
PercentDone float64 `json:"percent_done"`
|
||||||
|
TotalFiles uint64 `json:"total_files,omitempty"`
|
||||||
|
FilesDone uint64 `json:"files_done,omitempty"`
|
||||||
|
TotalBytes uint64 `json:"total_bytes,omitempty"`
|
||||||
|
BytesDone uint64 `json:"bytes_done,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type summaryOutput struct {
|
||||||
|
MessageType string `json:"message_type"` // "summary"
|
||||||
|
SecondsElapsed uint64 `json:"seconds_elapsed,omitempty"`
|
||||||
|
TotalFiles uint64 `json:"total_files,omitempty"`
|
||||||
|
FilesDone uint64 `json:"files_done,omitempty"`
|
||||||
|
TotalBytes uint64 `json:"total_bytes,omitempty"`
|
||||||
|
BytesDone uint64 `json:"bytes_done,omitempty"`
|
||||||
|
}
|
|
@ -11,7 +11,7 @@ type textPrinter struct {
|
||||||
terminal term
|
terminal term
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTextPrinter(terminal term) ProgressPrinter {
|
func NewTextProgress(terminal term) ProgressPrinter {
|
||||||
return &textPrinter{
|
return &textPrinter{
|
||||||
terminal: terminal,
|
terminal: terminal,
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,21 +21,21 @@ func (m *mockTerm) SetStatus(lines []string) {
|
||||||
|
|
||||||
func TestPrintUpdate(t *testing.T) {
|
func TestPrintUpdate(t *testing.T) {
|
||||||
term := &mockTerm{}
|
term := &mockTerm{}
|
||||||
printer := NewTextPrinter(term)
|
printer := NewTextProgress(term)
|
||||||
printer.Update(3, 11, 29, 47, 5*time.Second)
|
printer.Update(3, 11, 29, 47, 5*time.Second)
|
||||||
test.Equals(t, []string{"[0:05] 61.70% 3 files 29 B, total 11 files 47 B"}, term.output)
|
test.Equals(t, []string{"[0:05] 61.70% 3 files 29 B, total 11 files 47 B"}, term.output)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPrintSummaryOnSuccess(t *testing.T) {
|
func TestPrintSummaryOnSuccess(t *testing.T) {
|
||||||
term := &mockTerm{}
|
term := &mockTerm{}
|
||||||
printer := NewTextPrinter(term)
|
printer := NewTextProgress(term)
|
||||||
printer.Finish(11, 11, 47, 47, 5*time.Second)
|
printer.Finish(11, 11, 47, 47, 5*time.Second)
|
||||||
test.Equals(t, []string{"Summary: Restored 11 Files (47 B) in 0:05"}, term.output)
|
test.Equals(t, []string{"Summary: Restored 11 Files (47 B) in 0:05"}, term.output)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPrintSummaryOnErrors(t *testing.T) {
|
func TestPrintSummaryOnErrors(t *testing.T) {
|
||||||
term := &mockTerm{}
|
term := &mockTerm{}
|
||||||
printer := NewTextPrinter(term)
|
printer := NewTextProgress(term)
|
||||||
printer.Finish(3, 11, 29, 47, 5*time.Second)
|
printer.Finish(3, 11, 29, 47, 5*time.Second)
|
||||||
test.Equals(t, []string{"Summary: Restored 3 / 11 Files (29 B / 47 B) in 0:05"}, term.output)
|
test.Equals(t, []string{"Summary: Restored 3 / 11 Files (29 B / 47 B) in 0:05"}, term.output)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue