-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommand.go
99 lines (80 loc) · 1.92 KB
/
command.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
package main
import (
"bufio"
"bytes"
"encoding/json"
"os"
"os/exec"
codacy "github.com/codacy/codacy-engine-golang-seed/v6"
"strings"
)
const (
formatter = "ndjson"
)
// ReviveResult base structure of revive json result
type ReviveResult struct {
Failure string
RuleName string
Position RevivePosition
}
// RevivePosition revive json position
type RevivePosition struct {
Start RevivePositionResult
End RevivePositionResult
}
// RevivePositionResult revive json position result
type RevivePositionResult struct {
Filename string
Line int
Offset int
Column int
}
func getConfigFileParam(configFile *os.File) []string {
if configFile != nil {
return []string{
"-config",
configFile.Name(),
}
}
return []string{}
}
func commandParameters(configFile *os.File, filesToAnalyse []string) []string {
cmdParams := append(
[]string{
"-formatter", formatter,
},
getConfigFileParam(configFile)...,
)
cmdParams = append(cmdParams, filesToAnalyse...)
return cmdParams
}
func parseOutput(reviveOutput string) []codacy.Result {
var result []codacy.Result
scanner := bufio.NewScanner(strings.NewReader(reviveOutput))
for scanner.Scan() {
var reviveRes ReviveResult
json.Unmarshal([]byte(scanner.Text()), &reviveRes)
result = append(result, codacy.Issue{
PatternID: reviveRes.RuleName,
Message: reviveRes.Failure,
Line: reviveRes.Position.Start.Line,
File: reviveRes.Position.Start.Filename,
})
}
return result
}
func reviveCommand(configFile *os.File, filesToAnalyse []string, sourceDir string) *exec.Cmd {
params := commandParameters(configFile, filesToAnalyse)
cmd := exec.Command("revive", params...)
cmd.Dir = sourceDir
return cmd
}
func runCommand(cmd *exec.Cmd) (string, string, error) {
var stderr bytes.Buffer
cmd.Stderr = &stderr
cmdOutput, err := cmd.Output()
if err != nil {
return "", stderr.String(), err
}
return string(cmdOutput), "", nil
}