Files
steampipe/pkg/statushooks/snapshot_progress_reporter.go
2025-03-06 16:34:18 +05:30

53 lines
1.1 KiB
Go

package statushooks
import (
"context"
"fmt"
"strings"
"sync"
"github.com/turbot/pipe-fittings/v2/utils"
)
// SnapshotProgressReporter is an implementation of SnapshotProgress
type SnapshotProgressReporter struct {
rows int
errors int
name string
mut sync.Mutex
}
func NewSnapshotProgressReporter(target string) *SnapshotProgressReporter {
res := &SnapshotProgressReporter{
name: target,
}
return res
}
func (r *SnapshotProgressReporter) UpdateRowCount(ctx context.Context, rows int) {
r.mut.Lock()
defer r.mut.Unlock()
r.rows += rows
r.showProgress(ctx)
}
func (r *SnapshotProgressReporter) UpdateErrorCount(ctx context.Context, errors int) {
r.mut.Lock()
defer r.mut.Unlock()
r.errors += errors
r.showProgress(ctx)
}
func (r *SnapshotProgressReporter) showProgress(ctx context.Context) {
var msg strings.Builder
msg.WriteString(fmt.Sprintf("Running %s", r.name))
if r.rows > 0 {
msg.WriteString(fmt.Sprintf(", %d %s returned", r.rows, utils.Pluralize("row", r.rows)))
}
if r.errors > 0 {
msg.WriteString(fmt.Sprintf(", %d %s, ", r.errors, utils.Pluralize("error", r.errors)))
}
SetStatus(ctx, msg.String())
}