-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcodeclimate-golint.go
111 lines (92 loc) · 2.57 KB
/
codeclimate-golint.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
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"github.com/codeclimate/cc-engine-go/engine"
"golang.org/x/lint"
)
const defaultMinConfidence = 0.8
func main() {
rootPath := "/code/"
config, err := engine.LoadConfig()
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading config: %s", err)
os.Exit(1)
}
analysisFiles, err := engine.GoFileWalk(rootPath, engine.IncludePaths(rootPath, config))
if err != nil {
fmt.Fprintf(os.Stderr, "Error initializing: %s", err)
os.Exit(1)
}
minConfidence := getMinConfidence(config)
for _, path := range analysisFiles {
relativePath := strings.SplitAfter(path, rootPath)[1]
lintFile(path, relativePath, minConfidence)
}
}
func lintFile(fullPath string, relativePath string, minConfidence float64) {
linter := new(lint.Linter)
code, err := ioutil.ReadFile(fullPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not read file: %v\n", fullPath)
fmt.Fprintf(os.Stderr, "Error %v\n", err)
os.Exit(1)
}
problems, err := linter.Lint(fullPath, code)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not lint file: %v\n", fullPath)
fmt.Fprintf(os.Stderr, "Error %v\n", err)
os.Exit(1)
}
for _, problem := range problems {
if problem.Confidence < minConfidence {
continue
}
issue := &engine.Issue{
Type: "issue",
Check: codeClimateCheckName(&problem),
Description: (&problem).Text,
RemediationPoints: 50000,
Categories: []string{"Style"},
Location: codeClimateLocation(&problem, relativePath),
}
engine.PrintIssue(issue)
}
}
func codeClimateCheckName(l *lint.Problem) string {
return "GoLint/" + strings.Title(l.Category) + "/" + codeClimateIssueName(l)
}
func codeClimateIssueName(l *lint.Problem) string {
splitURL := strings.Split(l.Link, "#")
kebabLink := splitURL[len(splitURL)-1]
splitLink := strings.Split(kebabLink, "-")
for i, link := range splitLink {
splitLink[i] = strings.Title(link)
}
camelLink := strings.Join(splitLink, "")
return camelLink
}
func codeClimateLocation(l *lint.Problem, relativePath string) *engine.Location {
position := l.Position
return &engine.Location{
Path: relativePath,
Lines: &engine.LinesOnlyPosition{
Begin: position.Line,
End: position.Line,
},
}
}
func getMinConfidence(config engine.Config) float64 {
if subConfig, ok := config["config"].(map[string]interface{}); ok {
if minConfidence, ok := subConfig["min_confidence"].(string); ok {
val, err := strconv.ParseFloat(minConfidence, 64)
if err == nil {
return val
}
}
}
return defaultMinConfidence
}