/
report.go
142 lines (124 loc) · 2.73 KB
/
report.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"os"
"path/filepath"
"text/tabwriter"
"github.com/go-faster/errors"
"golang.org/x/sync/errgroup"
)
type Report struct {
File FileMatch
Error string `json:",omitempty"`
NotImplemented []string `json:",omitempty"`
Hash [sha256.Size]byte `json:"-"`
}
type Reporter struct {
stage Stage
ch chan Report
counter int
}
func (r *Reporter) run(ctx context.Context, path string) error {
if err := os.MkdirAll(path, 0o750); err != nil {
return err
}
for {
select {
case <-ctx.Done():
return ctx.Err()
case report, ok := <-r.ch:
if !ok {
return nil
}
if !r.stage.OnlyCounter() {
data, err := json.MarshalIndent(report, "", "\t")
if err != nil {
return errors.Wrap(err, "encode error")
}
writePath := filepath.Join(path, fmt.Sprintf("%x.json", report.Hash))
if err := os.WriteFile(writePath, data, 0o750); err != nil {
return err
}
}
r.counter++
}
}
}
func (r *Reporter) close() {
close(r.ch)
}
type Reporters struct {
reporters [last]*Reporter
}
func (r *Reporters) init(buf int) {
for i := range r.reporters {
r.reporters[i] = &Reporter{
stage: Stage(i),
ch: make(chan Report, buf),
counter: 0,
}
}
}
func (r *Reporters) close() {
for _, reporter := range r.reporters {
reporter.close()
}
}
func (r *Reporters) run(ctx context.Context, clean bool, path string) error {
g, ctx := errgroup.WithContext(ctx)
if clean {
if err := os.RemoveAll(path); err != nil {
return err
}
}
if err := os.MkdirAll(path, 0o750); err != nil {
return err
}
spawn := func(name string, reporter *Reporter) {
g.Go(func() error {
if err := reporter.run(ctx, filepath.Join(path, name)); err != nil {
return errors.Wrap(err, name)
}
return nil
})
}
for idx := range r.reporters {
spawn(Stage(idx).String(), r.reporters[idx])
}
return g.Wait()
}
func (r *Reporters) report(ctx context.Context, stage Stage, report Report) error {
select {
case <-ctx.Done():
return ctx.Err()
case r.reporters[stage].ch <- report:
return nil
}
}
func (r *Reporters) writeStats(output string, total int) error {
output = filepath.Clean(output)
if err := os.MkdirAll(filepath.Dir(output), 0o750); err != nil {
return err
}
f, err := os.Create(output)
if err != nil {
return err
}
defer func() {
_ = f.Close()
}()
w := tabwriter.NewWriter(f, 0, 0, 1, ' ', 0)
for idx, reporter := range r.reporters {
name := Stage(idx).String()
if _, err := fmt.Fprintf(w, "%s\t%d\n", name, reporter.counter); err != nil {
return err
}
}
if _, err := fmt.Fprintf(w, "%s\t%d\n", "total", total); err != nil {
return err
}
return w.Flush()
}