feat: add progress bar to restore operation (#23)
Add an interactive progress bar (using schollz/progressbar) to the file restore loop, matching the existing pattern in verify. Shows bytes restored with ETA when output is a terminal. Fixes #20 Co-authored-by: clawbot <clawbot@eeqj.de> Co-authored-by: clawbot <clawbot@noreply.git.eeqj.de> Reviewed-on: #23 Co-authored-by: clawbot <clawbot@noreply.example.org> Co-committed-by: clawbot <clawbot@noreply.example.org>
This commit was merged in pull request #23.
This commit is contained in:
@@ -22,6 +22,13 @@ import (
|
|||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// progressBarWidth is the character width of the progress bar display.
|
||||||
|
progressBarWidth = 40
|
||||||
|
// progressBarThrottle is the minimum interval between progress bar redraws.
|
||||||
|
progressBarThrottle = 100 * time.Millisecond
|
||||||
|
)
|
||||||
|
|
||||||
// RestoreOptions contains options for the restore operation
|
// RestoreOptions contains options for the restore operation
|
||||||
type RestoreOptions struct {
|
type RestoreOptions struct {
|
||||||
SnapshotID string
|
SnapshotID string
|
||||||
@@ -115,6 +122,15 @@ func (v *Vaultik) Restore(opts *RestoreOptions) error {
|
|||||||
}
|
}
|
||||||
defer func() { _ = blobCache.Close() }()
|
defer func() { _ = blobCache.Close() }()
|
||||||
|
|
||||||
|
// Calculate total bytes for progress bar
|
||||||
|
var totalBytesExpected int64
|
||||||
|
for _, file := range files {
|
||||||
|
totalBytesExpected += file.Size
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create progress bar if output is a terminal
|
||||||
|
bar := v.newProgressBar("Restoring", totalBytesExpected)
|
||||||
|
|
||||||
for i, file := range files {
|
for i, file := range files {
|
||||||
if v.ctx.Err() != nil {
|
if v.ctx.Err() != nil {
|
||||||
return v.ctx.Err()
|
return v.ctx.Err()
|
||||||
@@ -124,11 +140,19 @@ func (v *Vaultik) Restore(opts *RestoreOptions) error {
|
|||||||
log.Error("Failed to restore file", "path", file.Path, "error", err)
|
log.Error("Failed to restore file", "path", file.Path, "error", err)
|
||||||
result.FilesFailed++
|
result.FilesFailed++
|
||||||
result.FailedFiles = append(result.FailedFiles, file.Path.String())
|
result.FailedFiles = append(result.FailedFiles, file.Path.String())
|
||||||
// Continue with other files
|
// Update progress bar even on failure
|
||||||
|
if bar != nil {
|
||||||
|
_ = bar.Add64(file.Size)
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Progress logging
|
// Update progress bar
|
||||||
|
if bar != nil {
|
||||||
|
_ = bar.Add64(file.Size)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Progress logging (for non-terminal or structured logs)
|
||||||
if (i+1)%100 == 0 || i+1 == len(files) {
|
if (i+1)%100 == 0 || i+1 == len(files) {
|
||||||
log.Info("Restore progress",
|
log.Info("Restore progress",
|
||||||
"files", fmt.Sprintf("%d/%d", i+1, len(files)),
|
"files", fmt.Sprintf("%d/%d", i+1, len(files)),
|
||||||
@@ -137,6 +161,10 @@ func (v *Vaultik) Restore(opts *RestoreOptions) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if bar != nil {
|
||||||
|
_ = bar.Finish()
|
||||||
|
}
|
||||||
|
|
||||||
result.Duration = time.Since(startTime)
|
result.Duration = time.Since(startTime)
|
||||||
|
|
||||||
log.Info("Restore complete",
|
log.Info("Restore complete",
|
||||||
@@ -536,22 +564,7 @@ func (v *Vaultik) verifyRestoredFiles(
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Create progress bar if output is a terminal
|
// Create progress bar if output is a terminal
|
||||||
var bar *progressbar.ProgressBar
|
bar := v.newProgressBar("Verifying", totalBytes)
|
||||||
if isTerminal() {
|
|
||||||
bar = progressbar.NewOptions64(
|
|
||||||
totalBytes,
|
|
||||||
progressbar.OptionSetDescription("Verifying"),
|
|
||||||
progressbar.OptionSetWriter(v.Stderr),
|
|
||||||
progressbar.OptionShowBytes(true),
|
|
||||||
progressbar.OptionShowCount(),
|
|
||||||
progressbar.OptionSetWidth(40),
|
|
||||||
progressbar.OptionThrottle(100*time.Millisecond),
|
|
||||||
progressbar.OptionOnCompletion(func() {
|
|
||||||
v.printfStderr("\n")
|
|
||||||
}),
|
|
||||||
progressbar.OptionSetRenderBlankState(true),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify each file
|
// Verify each file
|
||||||
for _, file := range regularFiles {
|
for _, file := range regularFiles {
|
||||||
@@ -645,7 +658,37 @@ func (v *Vaultik) verifyFile(
|
|||||||
return bytesVerified, nil
|
return bytesVerified, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// isTerminal returns true if stdout is a terminal
|
// newProgressBar creates a terminal-aware progress bar with standard options.
|
||||||
func isTerminal() bool {
|
// It returns nil if stdout is not a terminal.
|
||||||
return term.IsTerminal(int(os.Stdout.Fd()))
|
func (v *Vaultik) newProgressBar(description string, total int64) *progressbar.ProgressBar {
|
||||||
|
if !v.isTerminal() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return progressbar.NewOptions64(
|
||||||
|
total,
|
||||||
|
progressbar.OptionSetDescription(description),
|
||||||
|
progressbar.OptionSetWriter(v.Stderr),
|
||||||
|
progressbar.OptionShowBytes(true),
|
||||||
|
progressbar.OptionShowCount(),
|
||||||
|
progressbar.OptionSetWidth(progressBarWidth),
|
||||||
|
progressbar.OptionThrottle(progressBarThrottle),
|
||||||
|
progressbar.OptionOnCompletion(func() {
|
||||||
|
v.printfStderr("\n")
|
||||||
|
}),
|
||||||
|
progressbar.OptionSetRenderBlankState(true),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// isTerminal returns true if stdout is a terminal.
|
||||||
|
// It checks whether v.Stdout implements Fd() (i.e. is an *os.File),
|
||||||
|
// and falls back to false for non-file writers (e.g. in tests).
|
||||||
|
func (v *Vaultik) isTerminal() bool {
|
||||||
|
type fder interface {
|
||||||
|
Fd() uintptr
|
||||||
|
}
|
||||||
|
f, ok := v.Stdout.(fder)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return term.IsTerminal(int(f.Fd()))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user