mirror of
https://github.com/gojp/goreportcard.git
synced 2026-01-28 22:39:05 +08:00
refactor to prepare for CLI
This commit is contained in:
102
check/check.go
102
check/check.go
@@ -1,5 +1,11 @@
|
||||
package check
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// Check describes what methods various checks (gofmt, go lint, etc.)
|
||||
// should implement
|
||||
type Check interface {
|
||||
@@ -10,3 +16,99 @@ type Check interface {
|
||||
// as well as a map of filename to output
|
||||
Percentage() (float64, []FileSummary, error)
|
||||
}
|
||||
|
||||
type Score struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
FileSummaries []FileSummary `json:"file_summaries"`
|
||||
Weight float64 `json:"weight"`
|
||||
Percentage float64 `json:"percentage"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
type ChecksResult struct {
|
||||
Checks []Score `json:"checks"`
|
||||
Average float64 `json:"average"`
|
||||
Grade Grade `json:"GradeFromPercentage"`
|
||||
Files int `json:"files"`
|
||||
Issues int `json:"issues"`
|
||||
}
|
||||
|
||||
func CheckDir(dir string) (ChecksResult, error) {
|
||||
filenames, skipped, err := GoFiles(dir)
|
||||
if err != nil {
|
||||
return ChecksResult{}, fmt.Errorf("could not get filenames: %v", err)
|
||||
}
|
||||
if len(filenames) == 0 {
|
||||
return ChecksResult{}, fmt.Errorf("no .go files found")
|
||||
}
|
||||
|
||||
err = RenameFiles(skipped)
|
||||
if err != nil {
|
||||
log.Println("Could not remove files:", err)
|
||||
}
|
||||
defer RevertFiles(skipped)
|
||||
|
||||
checks := []Check{
|
||||
GoFmt{Dir: dir, Filenames: filenames},
|
||||
GoVet{Dir: dir, Filenames: filenames},
|
||||
GoLint{Dir: dir, Filenames: filenames},
|
||||
GoCyclo{Dir: dir, Filenames: filenames},
|
||||
License{Dir: dir, Filenames: []string{}},
|
||||
Misspell{Dir: dir, Filenames: filenames},
|
||||
IneffAssign{Dir: dir, Filenames: filenames},
|
||||
// ErrCheck{Dir: dir, Filenames: filenames}, // disable errcheck for now, too slow and not finalized
|
||||
}
|
||||
|
||||
ch := make(chan Score)
|
||||
for _, c := range checks {
|
||||
go func(c Check) {
|
||||
p, summaries, err := c.Percentage()
|
||||
errMsg := ""
|
||||
if err != nil {
|
||||
log.Printf("ERROR: (%s) %v", c.Name(), err)
|
||||
errMsg = err.Error()
|
||||
}
|
||||
s := Score{
|
||||
Name: c.Name(),
|
||||
Description: c.Description(),
|
||||
FileSummaries: summaries,
|
||||
Weight: c.Weight(),
|
||||
Percentage: p,
|
||||
Error: errMsg,
|
||||
}
|
||||
ch <- s
|
||||
}(c)
|
||||
}
|
||||
|
||||
resp := ChecksResult{
|
||||
Files: len(filenames),
|
||||
}
|
||||
|
||||
var total, totalWeight float64
|
||||
var issues = make(map[string]bool)
|
||||
for i := 0; i < len(checks); i++ {
|
||||
s := <-ch
|
||||
resp.Checks = append(resp.Checks, s)
|
||||
total += s.Percentage * s.Weight
|
||||
totalWeight += s.Weight
|
||||
for _, fs := range s.FileSummaries {
|
||||
issues[fs.Filename] = true
|
||||
}
|
||||
}
|
||||
total /= totalWeight
|
||||
|
||||
sort.Sort(ByWeight(resp.Checks))
|
||||
resp.Average = total
|
||||
resp.Issues = len(issues)
|
||||
resp.Grade = GradeFromPercentage(total * 100)
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// ByWeight implements sorting for checks by weight descending
|
||||
type ByWeight []Score
|
||||
|
||||
func (a ByWeight) Len() int { return len(a) }
|
||||
func (a ByWeight) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
func (a ByWeight) Less(i, j int) bool { return a[i].Weight > a[j].Weight }
|
||||
|
||||
37
check/grade.go
Normal file
37
check/grade.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package check
|
||||
|
||||
// Grade represents a grade returned by the server, which is normally
|
||||
// somewhere between A+ (highest) and F (lowest).
|
||||
type Grade string
|
||||
|
||||
// The Grade constants below indicate the current available
|
||||
// grades.
|
||||
const (
|
||||
GradeAPlus Grade = "A+"
|
||||
GradeA = "A"
|
||||
GradeB = "B"
|
||||
GradeC = "C"
|
||||
GradeD = "D"
|
||||
GradeE = "E"
|
||||
GradeF = "F"
|
||||
)
|
||||
|
||||
// GradeFromPercentage is a helper for getting the GradeFromPercentage for a percentage
|
||||
func GradeFromPercentage(percentage float64) Grade {
|
||||
switch {
|
||||
case percentage > 90:
|
||||
return GradeAPlus
|
||||
case percentage > 80:
|
||||
return GradeA
|
||||
case percentage > 70:
|
||||
return GradeB
|
||||
case percentage > 60:
|
||||
return GradeC
|
||||
case percentage > 50:
|
||||
return GradeD
|
||||
case percentage > 40:
|
||||
return GradeE
|
||||
default:
|
||||
return GradeF
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user