-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummary.go
More file actions
63 lines (53 loc) · 1.79 KB
/
Copy pathsummary.go
File metadata and controls
63 lines (53 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package progress
import (
"context"
"fmt"
"io"
"os"
"sync"
)
// SummaryReporter writes only the start/end of the reportable activity and errors / warnings are printed as
// encountered.
type SummaryReporter struct {
DefaultWriter io.Writer
ErrorWriter io.Writer
Prefix string
mu sync.RWMutex
errorsReported uint
maxErrors uint
}
func NewSummaryReporter(prefix string) *SummaryReporter {
result := new(SummaryReporter)
result.DefaultWriter = os.Stdout
result.ErrorWriter = os.Stderr
result.Prefix = prefix
return result
}
func (pr *SummaryReporter) StartReportableActivity(ctx context.Context, summary string, expectedItems int) {
fmt.Fprintf(pr.DefaultWriter, "%s%s\n", pr.Prefix, summary)
}
func (pr *SummaryReporter) StartReportableReaderActivityInBytes(ctx context.Context, summary string, exepectedBytes int64, inputReader io.Reader) io.Reader {
fmt.Fprintf(pr.DefaultWriter, "%s%s\n", pr.Prefix, summary)
return inputReader
}
func (pr *SummaryReporter) IncrementReportableActivityProgress(ctx context.Context, incrementBy int) {
}
func (pr *SummaryReporter) CompleteReportableActivityProgress(ctx context.Context, summary string) {
fmt.Fprintf(pr.DefaultWriter, "%s%s\n", pr.Prefix, summary)
}
func (pr *SummaryReporter) CollectError(ctx context.Context, err error) bool {
pr.mu.Lock()
pr.errorsReported++
pr.mu.Unlock()
fmt.Fprintf(pr.ErrorWriter, "%s%v\n", pr.Prefix, err.Error())
return !pr.MaxErrorsCollected(ctx)
}
func (pr *SummaryReporter) MaxErrorsCollected(context.Context) bool {
pr.mu.RLock()
defer pr.mu.RUnlock()
return pr.maxErrors > 0 && pr.errorsReported > pr.maxErrors
}
func (pr *SummaryReporter) CollectWarning(ctx context.Context, code, message string) bool {
fmt.Fprintf(pr.DefaultWriter, "%s%s %s\n", pr.Prefix, code, message)
return true
}