generated from bool64/go-template
/
main.go
304 lines (238 loc) · 6.08 KB
/
main.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Package app provides sccdiff application entry point.
package app
import (
"bytes"
"encoding/csv"
"flag"
"fmt"
"io"
"log"
"os"
"sort"
"strconv"
"strings"
"code.cloudfoundry.org/bytefmt"
"github.com/bool64/dev/version"
"github.com/boyter/scc/v3/processor"
"github.com/olekukonko/tablewriter"
)
type flags struct {
baseRef string
baseDir string
all bool
groupDirs string
version bool
}
// Main is the application entry point.
func Main() {
f := flags{}
flag.StringVar(&f.baseRef, "baseref", "HEAD", "Base reference.")
flag.StringVar(&f.baseDir, "basedir", "", "Base directory.")
flag.BoolVar(&f.all, "all", false, "Include unmodified records in report.")
flag.BoolVar(&f.version, "version", false, "Show app version and exit.")
flag.StringVar(&f.groupDirs, "groupdirs", "", "Group files from directories, can be used to combine vendor, can have multiple comma separated values.")
flag.Parse()
if f.version {
fmt.Println(version.Info().Version)
return
}
processor.Format = "csv"
processor.MinifiedGenerated = true
processor.GeneratedMarkers = []string{"do not edit", "<auto-generated />"}
processor.Files = true
var groupDirs []string
if f.groupDirs != "" {
groupDirs = strings.Split(f.groupDirs, ",")
}
current, err := getResults("", groupDirs)
if err != nil {
log.Fatal(err)
return
}
currentGrouped := make(map[string]resultRow)
for _, v := range current {
currentGrouped[v.Language] = currentGrouped[v.Language].add(v)
}
do := func(workPath string) {
processor.DirFilePaths = []string{workPath}
base, err := getResults(workPath+"/", groupDirs)
if err != nil {
log.Fatal(err)
return
}
for _, v := range base {
currentGrouped[v.Language] = currentGrouped[v.Language].addBase(v)
}
}
if f.baseDir == "" {
err = runAtGitRef(nil, "git", "", f.baseRef, do)
if err != nil {
println(err.Error())
return
}
} else {
do(f.baseDir)
}
printTable(currentGrouped, f.all)
}
func printTable(currentGrouped map[string]resultRow, all bool) {
data := make([][]string, 0, len(currentGrouped))
for _, v := range currentGrouped {
hasDiff := false
row := []string{
v.Language,
format(v.Files, v.FilesBase, &hasDiff),
format(v.Lines, v.LinesBase, &hasDiff),
format(v.Code, v.CodeBase, &hasDiff),
format(v.Comments, v.CommentsBase, &hasDiff),
format(v.Blanks, v.BlanksBase, &hasDiff),
format(v.Complexity, v.ComplexityBase, &hasDiff),
formatBytes(v.Bytes, v.BytesBase, &hasDiff),
}
if !hasDiff && !all {
continue
}
data = append(data, row)
}
sort.Slice(data, func(i, j int) bool {
return data[i][0] < data[j][0]
})
table := tablewriter.NewWriter(os.Stdout)
table.SetAutoFormatHeaders(false)
table.SetHeader([]string{"Language", "Files", "Lines", "Code", "Comments", "Blanks", "Complexity", "Bytes"})
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
table.AppendBulk(data) // Add Bulk Data
table.Render()
}
func formatBytes(current, base int, hasDiff *bool) string {
if base > current {
*hasDiff = true
return fmt.Sprintf("%s (-%s)", bytefmt.ByteSize(uint64(current)), bytefmt.ByteSize(uint64(base-current)))
} else if base < current {
*hasDiff = true
return fmt.Sprintf("%s (+%s)", bytefmt.ByteSize(uint64(current)), bytefmt.ByteSize(uint64(current-base)))
}
return bytefmt.ByteSize(uint64(current))
}
func format(current, base int, hasDiff *bool) string {
if base > current {
*hasDiff = true
return fmt.Sprintf("%d (-%d)", current, base-current)
} else if base < current {
*hasDiff = true
return fmt.Sprintf("%d (+%d)", current, current-base)
}
return fmt.Sprintf("%d", current)
}
type resultRow struct {
Language string
Location string
Filename string
Files int
Lines int
Code int
Comments int
Blanks int
Complexity int
Bytes int
FilesBase int
LinesBase int
CodeBase int
CommentsBase int
BlanksBase int
ComplexityBase int
BytesBase int
}
func (gv resultRow) add(v resultRow) resultRow {
gv.Language = v.Language
gv.Files++
gv.Lines += v.Lines
gv.Code += v.Code
gv.Comments += v.Comments
gv.Blanks += v.Blanks
gv.Complexity += v.Complexity
gv.Bytes += v.Bytes
return gv
}
func (gv resultRow) addBase(v resultRow) resultRow {
gv.Language = v.Language
gv.FilesBase++
gv.LinesBase += v.Lines
gv.CodeBase += v.Code
gv.CommentsBase += v.Comments
gv.BlanksBase += v.Blanks
gv.ComplexityBase += v.Complexity
gv.BytesBase += v.Bytes
return gv
}
func getResults(stripPath string, groupDirs []string) (map[string]resultRow, error) {
out, err := processOutput()
if err != nil {
return nil, err
}
cr := csv.NewReader(bytes.NewReader(out))
s, err := cr.ReadAll()
if err != nil {
return nil, err
}
result := make(map[string]resultRow, len(s))
for i := 1; i < len(s); i++ {
c := s[i]
row := resultRow{
Language: c[0],
Location: c[1],
Filename: c[2],
Lines: mustParseInt(c[3]),
Code: mustParseInt(c[4]),
Comments: mustParseInt(c[5]),
Blanks: mustParseInt(c[6]),
Complexity: mustParseInt(c[7]),
Bytes: mustParseInt(c[8]),
}
row.Location = strings.TrimPrefix(row.Location, stripPath)
if strings.HasSuffix(row.Filename, "_test.go") {
row.Language += " (test)"
}
for _, dir := range groupDirs {
if strings.HasPrefix(row.Location, dir+"/") {
row.Language = dir
}
}
result[row.Location] = row
}
return result, nil
}
func mustParseInt(s string) int {
i, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return i
}
func processOutput() ([]byte, error) {
old := os.Stdout // keep backup of the real stdout
r, w, err := os.Pipe()
if err != nil {
return nil, err
}
os.Stdout = w
defer func() { os.Stdout = old }()
buf := bytes.NewBuffer(nil)
done := make(chan struct{})
var copyErr error
go func() {
_, copyErr = io.Copy(buf, r)
close(done)
}()
processor.Process()
if err = w.Close(); err != nil {
return nil, err
}
<-done
if copyErr != nil {
return nil, copyErr
}
return buf.Bytes(), nil
}