/
strategy_gofmt.go
71 lines (61 loc) · 1.85 KB
/
strategy_gofmt.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
package engine
import (
"fmt"
"github.com/360EntSecGroup-Skylar/goreporter/linters/gofmt"
"github.com/360EntSecGroup-Skylar/goreporter/utils"
)
type StrategyGoFmt struct {
Sync *Synchronizer `inject:""`
}
func (s *StrategyGoFmt) GetName() string {
return "GoFmt"
}
func (s *StrategyGoFmt) GetDescription() string {
return `go fmt formats Go programs. We run gofmt -s on your code, where -s is for the "simplify" command.`
}
func (s *StrategyGoFmt) GetWeight() float64 {
return 0.05
}
func (s *StrategyGoFmt) Compute(parameters StrategyParameter) (summaries *Summaries) {
summaries = NewSummaries()
slicePackagePaths := make([]string, 0)
for _, packagePath := range parameters.AllDirs {
slicePackagePaths = append(slicePackagePaths, packagePath)
}
lints, err := gofmt.GoFmt(slicePackagePaths)
if err != nil {
fmt.Println(err)
}
sumProcessNumber := int64(10)
processUnit := utils.GetProcessUnit(sumProcessNumber, len(lints))
for _, lintTip := range lints {
packageName := utils.PackageNameFromGoPath(lintTip)
erroru := Error{
LineNumber: 1,
ErrorString: utils.AbsPath(lintTip) + ":warning: file is not gofmted with -s (gofmt)",
}
summaries.Lock()
if summarie, ok := summaries.Summaries[packageName]; ok {
summarie.Errors = append(summarie.Errors, erroru)
summaries.Summaries[packageName] = summarie
} else {
summarie := Summary{
Name: packageName,
Errors: make([]Error, 0),
}
summarie.Errors = append(summarie.Errors, erroru)
summaries.Summaries[packageName] = summarie
}
summaries.Unlock()
if sumProcessNumber > 0 {
s.Sync.LintersProcessChans <- processUnit
sumProcessNumber = sumProcessNumber - processUnit
}
}
return summaries
}
func (s *StrategyGoFmt) Percentage(summaries *Summaries) float64 {
summaries.RLock()
defer summaries.RUnlock()
return utils.CountPercentage(len(summaries.Summaries))
}