mirror of
https://github.com/restic/restic.git
synced 2025-12-03 20:51:47 +00:00
restore: add basic json progress
This commit is contained in:
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
|
||||
}
|
||||
|
||||
func NewTextPrinter(terminal term) ProgressPrinter {
|
||||
func NewTextProgress(terminal term) ProgressPrinter {
|
||||
return &textPrinter{
|
||||
terminal: terminal,
|
||||
}
|
||||
|
||||
@@ -21,21 +21,21 @@ func (m *mockTerm) SetStatus(lines []string) {
|
||||
|
||||
func TestPrintUpdate(t *testing.T) {
|
||||
term := &mockTerm{}
|
||||
printer := NewTextPrinter(term)
|
||||
printer := NewTextProgress(term)
|
||||
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)
|
||||
}
|
||||
|
||||
func TestPrintSummaryOnSuccess(t *testing.T) {
|
||||
term := &mockTerm{}
|
||||
printer := NewTextPrinter(term)
|
||||
printer := NewTextProgress(term)
|
||||
printer.Finish(11, 11, 47, 47, 5*time.Second)
|
||||
test.Equals(t, []string{"Summary: Restored 11 Files (47 B) in 0:05"}, term.output)
|
||||
}
|
||||
|
||||
func TestPrintSummaryOnErrors(t *testing.T) {
|
||||
term := &mockTerm{}
|
||||
printer := NewTextPrinter(term)
|
||||
printer := NewTextProgress(term)
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user