1
0
Fork 0
mirror of https://github.com/restic/restic.git synced 2024-12-23 08:16:36 +00:00
restic/internal/ui/restore/json.go

78 lines
2.2 KiB
Go
Raw Normal View History

2023-05-01 10:01:03 +00:00
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))
}
2024-05-31 11:43:57 +00:00
func (t *jsonPrinter) Update(p State, duration time.Duration) {
2023-05-01 10:01:03 +00:00
status := statusUpdate{
MessageType: "status",
SecondsElapsed: uint64(duration / time.Second),
2024-05-31 11:43:57 +00:00
TotalFiles: p.FilesTotal,
FilesRestored: p.FilesFinished,
FilesSkipped: p.FilesSkipped,
2024-05-31 11:43:57 +00:00
TotalBytes: p.AllBytesTotal,
BytesRestored: p.AllBytesWritten,
BytesSkipped: p.AllBytesSkipped,
2023-05-01 10:01:03 +00:00
}
2024-05-31 11:43:57 +00:00
if p.AllBytesTotal > 0 {
status.PercentDone = float64(p.AllBytesWritten) / float64(p.AllBytesTotal)
2023-05-01 10:01:03 +00:00
}
t.print(status)
}
2024-05-31 11:43:57 +00:00
func (t *jsonPrinter) Finish(p State, duration time.Duration) {
2023-05-01 10:01:03 +00:00
status := summaryOutput{
MessageType: "summary",
SecondsElapsed: uint64(duration / time.Second),
2024-05-31 11:43:57 +00:00
TotalFiles: p.FilesTotal,
FilesRestored: p.FilesFinished,
FilesSkipped: p.FilesSkipped,
2024-05-31 11:43:57 +00:00
TotalBytes: p.AllBytesTotal,
BytesRestored: p.AllBytesWritten,
BytesSkipped: p.AllBytesSkipped,
2023-05-01 10:01:03 +00:00
}
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"`
FilesRestored uint64 `json:"files_restored,omitempty"`
FilesSkipped uint64 `json:"files_skipped,omitempty"`
2023-05-01 10:01:03 +00:00
TotalBytes uint64 `json:"total_bytes,omitempty"`
BytesRestored uint64 `json:"bytes_restored,omitempty"`
BytesSkipped uint64 `json:"bytes_skipped,omitempty"`
2023-05-01 10:01:03 +00:00
}
type summaryOutput struct {
MessageType string `json:"message_type"` // "summary"
SecondsElapsed uint64 `json:"seconds_elapsed,omitempty"`
TotalFiles uint64 `json:"total_files,omitempty"`
FilesRestored uint64 `json:"files_restored,omitempty"`
FilesSkipped uint64 `json:"files_skipped,omitempty"`
2023-05-01 10:01:03 +00:00
TotalBytes uint64 `json:"total_bytes,omitempty"`
BytesRestored uint64 `json:"bytes_restored,omitempty"`
BytesSkipped uint64 `json:"bytes_skipped,omitempty"`
2023-05-01 10:01:03 +00:00
}