-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdartanalyzerRunner.go
137 lines (119 loc) · 3.24 KB
/
dartanalyzerRunner.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
package tools
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
)
const patternPrefix = "dartanalyzer_"
func RunDartAnalyzer(workDirectory string, installationDirectory string, binary string, files []string, outputFile string, outputFormat string) error {
configFiles := []string{"analysis_options.yaml", "analysis_options.yml"}
dartAnalyzerPath := filepath.Join(installationDirectory, "bin", "dart")
args := []string{"analyze", "--format", "machine"}
// Add files to analyze - if no files specified, analyze current directory
if len(files) > 0 {
args = append(args, files...)
} else {
args = append(args, ".")
}
cmd := exec.Command(dartAnalyzerPath, args...)
cmd.Dir = workDirectory
// Check if any config file exists
configExists := false
for _, configFile := range configFiles {
if _, err := os.Stat(filepath.Join(workDirectory, configFile)); err == nil {
configExists = true
break
}
}
if !configExists {
fmt.Println("No config file found, using tool defaults")
} else {
fmt.Println("Config file found, using it")
}
// For SARIF output, we need to capture the output and transform it
if outputFormat == "sarif" {
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
cmd.Run()
// Convert Dart Analyzer output to SARIF format
sarif := map[string]interface{}{
"$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
"runs": []map[string]interface{}{
{
"tool": map[string]interface{}{
"driver": map[string]interface{}{
"name": "dartanalyzer",
},
},
"results": []map[string]interface{}{},
},
},
}
// Parse Dart Analyzer output and convert to SARIF
// Format is typically: file:line:col: severity: message
scanner := bufio.NewScanner(strings.NewReader(stdout.String()))
for scanner.Scan() {
line := scanner.Text()
if line == "" {
continue
}
// Split line into fields
fields := strings.Split(line, "|")
if len(fields) < 8 {
continue
}
// Extract fields
file := fields[3]
lineNum, _ := strconv.Atoi(fields[4])
message := fields[7]
ruleId := fields[2]
// Create result object
result := map[string]interface{}{
"message": map[string]string{
"text": message,
},
"locations": []map[string]interface{}{
{
"physicalLocation": map[string]interface{}{
"artifactLocation": map[string]interface{}{
"uri": file,
},
"region": map[string]interface{}{
"startLine": lineNum,
},
},
},
},
"ruleId": ruleId,
}
// Add result to SARIF output
sarif["runs"].([]map[string]interface{})[0]["results"] = append(
sarif["runs"].([]map[string]interface{})[0]["results"].([]map[string]interface{}),
result,
)
}
// Write SARIF output to file if specified
if outputFile != "" {
sarifJson, _ := json.MarshalIndent(sarif, "", " ")
err := os.WriteFile(outputFile, sarifJson, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "Error writing SARIF output: %v\n", err)
}
}
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Run()
} else {
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Run()
}
return nil
}