github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/statushooks/snapshot_progress_reporter.go (about)

     1  package statushooks
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"github.com/turbot/steampipe/pkg/utils"
     7  	"strings"
     8  	"sync"
     9  )
    10  
    11  // SnapshotProgressReporter is an implementation of SnapshotProgress
    12  type SnapshotProgressReporter struct {
    13  	rows   int
    14  	errors int
    15  	name   string
    16  	mut    sync.Mutex
    17  }
    18  
    19  func NewSnapshotProgressReporter(target string) *SnapshotProgressReporter {
    20  	res := &SnapshotProgressReporter{
    21  		name: target,
    22  	}
    23  	return res
    24  }
    25  
    26  func (r *SnapshotProgressReporter) UpdateRowCount(ctx context.Context, rows int) {
    27  	r.mut.Lock()
    28  	defer r.mut.Unlock()
    29  
    30  	r.rows += rows
    31  	r.showProgress(ctx)
    32  }
    33  func (r *SnapshotProgressReporter) UpdateErrorCount(ctx context.Context, errors int) {
    34  	r.mut.Lock()
    35  	defer r.mut.Unlock()
    36  	r.errors += errors
    37  	r.showProgress(ctx)
    38  }
    39  
    40  func (r *SnapshotProgressReporter) showProgress(ctx context.Context) {
    41  	var msg strings.Builder
    42  	msg.WriteString(fmt.Sprintf("Running %s", r.name))
    43  	if r.rows > 0 {
    44  		msg.WriteString(fmt.Sprintf(", %d %s returned", r.rows, utils.Pluralize("row", r.rows)))
    45  	}
    46  	if r.errors > 0 {
    47  		msg.WriteString(fmt.Sprintf(", %d %s, ", r.errors, utils.Pluralize("error", r.errors)))
    48  	}
    49  
    50  	SetStatus(ctx, msg.String())
    51  }