-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
183 lines (147 loc) · 3.57 KB
/
main.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"github.com/fatih/color"
"os"
"os/exec"
"path"
"strings"
)
type Sarif struct {
Runs []SarifRun
}
type SarifRun struct {
Results []SarifResult
}
type SarifResult struct {
Message SarifMessage
Locations []SarifLocation
}
type SarifMessage struct {
Text string
}
type SarifLocation struct {
PhysicalLocation SarifPhysicalLocation
}
type SarifPhysicalLocation struct {
ArtifactLocation SarifArtifactLocation
Region SarifRegion
}
type SarifRegion struct {
StartLine int
StartColumn int
EndLine int
}
type SarifArtifactLocation struct {
Uri string
}
type Finding struct {
Rule string `json:"rule"`
Message string `json:"message"`
File string `json:"file"`
Line int `json:"line"`
Severity string `json:"severity"`
}
func main() {
dir := flag.String("dir", ".", "Directory to analyze")
command := flag.String("command", "", "Custom build command")
flag.Parse()
if err := runMigrationCheck(*dir, *command); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
func runMigrationCheck(dir, customBuildCommand string) error {
dbPath, err := os.MkdirTemp(os.TempDir(), "cosmos-migration-db")
if err != nil {
return err
}
defer os.RemoveAll(dbPath)
command := []string{
"codeql",
"database",
"create",
"--language=go",
"--source-root", dir,
}
if customBuildCommand != "" {
command = append(command, "--command", customBuildCommand)
fmt.Println("Using custom build command:", customBuildCommand)
}
command = append(command, dbPath)
fmt.Println(command)
cmd := exec.Command(command[0], command[1:]...)
if err := cmd.Run(); err != nil {
return err
}
results, err := runAnalysis(dbPath)
if err != nil {
return err
}
return printFindings(results)
}
func runAnalysis(dbPath string) (*Sarif, error) {
// Run CodeQL analysis with your custom pack
resultsPath := path.Join(dbPath, "results.json")
cmd := exec.Command("codeql", "database", "analyze",
"--format=sarif-latest",
fmt.Sprintf("--output=%s", resultsPath),
dbPath,
"skip-mev/cosmos-52-ql")
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("analysis failed: %w", err)
}
// Parse results
data, err := os.ReadFile(resultsPath)
if err != nil {
return nil, err
}
var sarif Sarif
if err := json.Unmarshal(data, &sarif); err != nil {
return nil, err
}
return &sarif, nil
}
func printFindings(sarif *Sarif) error {
red := color.New(color.FgRed)
yellow := color.New(color.FgYellow)
for _, run := range sarif.Runs {
for _, result := range run.Results {
for _, location := range result.Locations {
uri := location.PhysicalLocation.ArtifactLocation.Uri
line := location.PhysicalLocation.Region.StartLine
column := location.PhysicalLocation.Region.StartColumn
code, err := readSpecificLine(uri, line)
if err != nil {
return fmt.Errorf("failed to read file: %w", err)
}
fmt.Printf("%s:%d:%d: %s\n", uri, line, column, red.Sprint(result.Message.Text))
fmt.Printf(" %d: %s\n", line, code)
fmt.Println(strings.Repeat(" ", column) + yellow.Sprint("^"))
}
}
}
return nil
}
func readSpecificLine(filePath string, targetLine int) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", err
}
defer file.Close()
scanner := bufio.NewScanner(file)
currentLine := 1
for scanner.Scan() {
if currentLine == targetLine {
return scanner.Text(), nil
}
currentLine++
}
if err := scanner.Err(); err != nil {
return "", err
}
return "", fmt.Errorf("file has less than %d lines", targetLine)
}