-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
505 lines (429 loc) · 13 KB
/
main.go
File metadata and controls
505 lines (429 loc) · 13 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
package main
import (
"bytes"
_ "embed"
"encoding/json"
"fmt"
"html/template"
"io"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
)
var (
shfmtPath = getEnvOrDefault("SHFMT_PATH", "shfmt")
shellcheckPath = getEnvOrDefault("SHELLCHECK_PATH", "shellcheck")
groqAPIKey = os.Getenv("GROQ_API_KEY")
groqModelID = getEnvOrDefault("GROQ_MODEL_ID", "openai/gpt-oss-120b")
groqAPIURL = getEnvOrDefault("GROQ_API_URL", "https://api.groq.com/openai/v1/chat/completions")
)
//go:embed index.html
var indexHTML string
type ShellcheckResponse struct {
HTML string `json:"html"`
Annotations []Annotation `json:"annotations"`
}
type Annotation struct {
Row int `json:"row"`
Column int `json:"column"`
Text string `json:"text"`
Type string `json:"type"`
}
type LineError struct {
Code string
Severity string
Message string
Column int
}
func getEnvOrDefault(key, defaultValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defaultValue
}
func main() {
http.HandleFunc("/", handleIndex)
http.HandleFunc("/format", handleFormat)
http.HandleFunc("/shellcheck", handleShellcheck)
http.HandleFunc("/autofix", handleAutofix)
http.HandleFunc("/autofix-ai", handleAutofixAI)
port := getEnvOrDefault("PORT", "8085")
log.Printf("Server starting on http://localhost:%s", port)
log.Printf("Using shfmt: %s", shfmtPath)
log.Printf("Using shellcheck: %s", shellcheckPath)
if groqAPIKey != "" {
log.Printf("AI autofix enabled with model: %s", groqModelID)
}
log.Fatal(http.ListenAndServe(":"+port, nil))
}
func handleIndex(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.New("index").Parse(indexHTML))
tmpl.Execute(w, nil)
}
func handleFormat(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
code := r.FormValue("code")
if code == "" {
w.Write([]byte(code))
return
}
cmd := exec.Command(shfmtPath, "-")
cmd.Stdin = bytes.NewBufferString(code)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
log.Printf("shfmt error: %v", err)
w.Write([]byte(code))
return
}
w.Write(out.Bytes())
}
func handleShellcheck(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
code := r.FormValue("code")
if code == "" {
respondJSON(w, ShellcheckResponse{
HTML: `<div class="text-sm text-zinc-500">No code to check</div>`,
Annotations: []Annotation{},
})
return
}
// Create temporary file for shellcheck
tmpFile := filepath.Join(os.TempDir(), "script.sh")
if err := os.WriteFile(tmpFile, []byte(code), 0644); err != nil {
respondJSON(w, ShellcheckResponse{
HTML: fmt.Sprintf(`<div class="text-sm text-red-600">Error: %v</div>`, err),
Annotations: []Annotation{},
})
return
}
defer os.Remove(tmpFile)
// Run shellcheck
cmd := exec.Command(shellcheckPath, "-f", "tty", tmpFile)
var out, stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
cmd.Run()
output := out.String()
if output == "" {
output = stderr.String()
}
respondJSON(w, ShellcheckResponse{
HTML: formatShellcheckHTML(output),
Annotations: parseShellcheckOutput(output),
})
}
func respondJSON(w http.ResponseWriter, data interface{}) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(data)
}
func parseShellcheckOutput(output string) []Annotation {
annotations := []Annotation{}
lineErrors := make(map[int][]LineError)
lines := regexp.MustCompile(`\r?\n`).Split(output, -1)
scCodeRegex := regexp.MustCompile(`(SC\d+)\s+\((error|warning|info|style)\):\s*(.+)`)
lineRegex := regexp.MustCompile(`\bline\s+(\d+):`)
columnRegex := regexp.MustCompile(`^(\s*)\^`)
var currentLine int
var currentColumn int
for _, line := range lines {
// Extract line number
if lineMatch := lineRegex.FindStringSubmatch(line); len(lineMatch) >= 2 {
if num, err := strconv.Atoi(lineMatch[1]); err == nil {
currentLine = num
currentColumn = 0
}
}
// Extract column position from ^-- marker
if colMatch := columnRegex.FindStringSubmatch(line); len(colMatch) > 1 {
currentColumn = len(colMatch[1])
}
// Extract error code and message
if scMatch := scCodeRegex.FindStringSubmatch(line); currentLine > 0 && len(scMatch) >= 4 {
lineErrors[currentLine] = append(lineErrors[currentLine], LineError{
Code: scMatch[1],
Severity: scMatch[2],
Message: scMatch[3],
Column: currentColumn,
})
}
}
// Create annotations grouped by line
for lineNum, errors := range lineErrors {
if len(errors) == 0 {
continue
}
// Determine annotation type based on most severe error
annotationType := "info"
column := 0
for _, err := range errors {
if err.Severity == "error" {
annotationType = "error"
break
} else if err.Severity == "warning" && annotationType != "error" {
annotationType = "warning"
}
}
// Use column from first error (they should all be the same for a given line)
if len(errors) > 0 {
column = errors[0].Column
}
// Build combined error message with one line per issue
var messages []string
for _, err := range errors {
messages = append(messages, fmt.Sprintf("%s: %s", err.Code, err.Message))
}
annotations = append(annotations, Annotation{
Row: lineNum - 1, // Ace uses 0-based indexing
Column: column,
Text: strings.Join(messages, "\n"),
Type: annotationType,
})
}
return annotations
}
func handleAutofix(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
code := r.FormValue("code")
if code == "" {
w.Write([]byte(code))
return
}
// Create temporary file for shellcheck
tmpFile := filepath.Join(os.TempDir(), "script.sh")
if err := os.WriteFile(tmpFile, []byte(code), 0644); err != nil {
log.Printf("autofix error: %v", err)
w.Write([]byte(code))
return
}
defer os.Remove(tmpFile)
// Run shellcheck with --format=diff to get fixes
cmd := exec.Command(shellcheckPath, "-f", "diff", tmpFile)
var out, stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
cmd.Run()
diff := out.String()
if diff == "" {
// No fixes available, return original code
w.Write([]byte(code))
return
}
// Apply the diff using patch
patchCmd := exec.Command("patch", tmpFile)
patchCmd.Stdin = bytes.NewBufferString(diff)
var patchStderr bytes.Buffer
patchCmd.Stderr = &patchStderr
if err := patchCmd.Run(); err != nil {
log.Printf("patch error: %v - %s", err, patchStderr.String())
w.Write([]byte(code))
return
}
// Read the fixed file
fixed, err := os.ReadFile(tmpFile)
if err != nil {
log.Printf("read error: %v", err)
w.Write([]byte(code))
return
}
w.Write(fixed)
}
type GroqRequest struct {
Model string `json:"model"`
Temperature float64 `json:"temperature"`
Messages []GroqMessage `json:"messages"`
ResponseFormat map[string]interface{} `json:"response_format"`
}
type GroqMessage struct {
Role string `json:"role"`
Content string `json:"content"`
}
type GroqResponse struct {
Choices []struct {
Message struct {
Content string `json:"content"`
} `json:"message"`
} `json:"choices"`
}
type FixedCodeResponse struct {
FixedCode string `json:"fixed_code"`
}
func handleAutofixAI(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
if groqAPIKey == "" {
log.Printf("GROQ_API_KEY not set")
http.Error(w, "AI autofix not configured", http.StatusInternalServerError)
return
}
code := r.FormValue("code")
if code == "" {
w.Write([]byte(code))
return
}
// Create temporary file for shellcheck
tmpFile := filepath.Join(os.TempDir(), "script.sh")
if err := os.WriteFile(tmpFile, []byte(code), 0644); err != nil {
log.Printf("autofix-ai error: %v", err)
w.Write([]byte(code))
return
}
defer os.Remove(tmpFile)
// Run shellcheck to get issues
cmd := exec.Command(shellcheckPath, "-f", "tty", tmpFile)
var out, stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
cmd.Run()
shellcheckOutput := out.String()
if shellcheckOutput == "" {
shellcheckOutput = stderr.String()
}
if shellcheckOutput == "" {
// No issues to fix
w.Write([]byte(code))
return
}
// Build prompt for AI
prompt := fmt.Sprintf(`Fix all ShellCheck issues in the following bash script. Return ONLY the fixed code without any explanations, markdown formatting, or code blocks.
ShellCheck Issues:
%s
Original Script:
%s`, shellcheckOutput, code)
// Prepare Groq API request
reqBody := GroqRequest{
Model: groqModelID,
Temperature: 0,
Messages: []GroqMessage{
{
Role: "system",
Content: "You are a bash script fixing assistant. Return only the fixed code without any markdown formatting or explanations.",
},
{
Role: "user",
Content: prompt,
},
},
ResponseFormat: map[string]interface{}{
"type": "json_schema",
"json_schema": map[string]interface{}{
"name": "fixed_script",
"schema": map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"fixed_code": map[string]interface{}{
"type": "string",
},
},
"required": []string{"fixed_code"},
},
},
},
}
jsonData, err := json.Marshal(reqBody)
if err != nil {
log.Printf("JSON marshal error: %v", err)
w.Write([]byte(code))
return
}
// Call Groq API
req, err := http.NewRequest("POST", groqAPIURL, bytes.NewBuffer(jsonData))
if err != nil {
log.Printf("Request creation error: %v", err)
w.Write([]byte(code))
return
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+groqAPIKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Printf("API request error: %v", err)
w.Write([]byte(code))
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
log.Printf("API error: %d %s: %s", resp.StatusCode, resp.Status, string(body))
w.Write([]byte(code))
return
}
var groqResp GroqResponse
if err := json.NewDecoder(resp.Body).Decode(&groqResp); err != nil {
log.Printf("JSON decode error: %v", err)
w.Write([]byte(code))
return
}
if len(groqResp.Choices) == 0 {
log.Printf("No choices in response")
w.Write([]byte(code))
return
}
var fixedResp FixedCodeResponse
if err := json.Unmarshal([]byte(groqResp.Choices[0].Message.Content), &fixedResp); err != nil {
log.Printf("Fixed code parse error: %v", err)
w.Write([]byte(code))
return
}
w.Write([]byte(fixedResp.FixedCode))
}
func formatShellcheckHTML(output string) string {
if output == "" {
return `<div class="text-sm text-green-600">✓ No issues found</div>`
}
// Remove the full file path from "In /path/to/file line X:" to just "Line X:"
pathRegex := regexp.MustCompile(`In .+/script\.sh line (\d+):`)
output = pathRegex.ReplaceAllString(output, `Line $1:`)
// Remove "For more information:" section
lines := regexp.MustCompile(`\r?\n`).Split(output, -1)
var filteredLines []string
skipMode := false
for _, line := range lines {
if regexp.MustCompile(`(?i)^For more information:`).MatchString(line) {
skipMode = true
continue
}
if skipMode && regexp.MustCompile(`^\s+https://`).MatchString(line) {
continue
}
if skipMode && !regexp.MustCompile(`^\s+https://`).MatchString(line) && line != "" {
skipMode = false
}
if !skipMode {
filteredLines = append(filteredLines, line)
}
}
formatted := strings.Join(filteredLines, "\n")
// Make SC codes clickable
scCodeRegex := regexp.MustCompile(`(SC\d+)`)
formatted = scCodeRegex.ReplaceAllStringFunc(formatted, func(code string) string {
return fmt.Sprintf(`<a href="https://www.shellcheck.net/wiki/%s" target="_blank" class="text-blue-400 hover:text-blue-300 underline">%s</a>`, code, code)
})
// Color-code severity levels
formatted = regexp.MustCompile(`(?m)^(.+SC\d+.+\(error\):.+)$`).ReplaceAllString(formatted, `<span class="text-red-400">$1</span>`)
formatted = regexp.MustCompile(`(?m)^(.+SC\d+.+\(warning\):.+)$`).ReplaceAllString(formatted, `<span class="text-yellow-400">$1</span>`)
formatted = regexp.MustCompile(`(?m)^(.+SC\d+.+\(info\):.+)$`).ReplaceAllString(formatted, `<span class="text-blue-400">$1</span>`)
formatted = regexp.MustCompile(`(?m)^(.+SC\d+.+\(style\):.+)$`).ReplaceAllString(formatted, `<span class="text-green-400">$1</span>`)
// Color line numbers and make them clickable (now just "Line X:")
formatted = regexp.MustCompile(`(?m)^Line (\d+):`).ReplaceAllString(formatted, `<a href="#" class="line-link text-cyan-400 hover:text-cyan-300 cursor-pointer underline" data-line="$1">Line $1:</a>`)
return fmt.Sprintf(`<pre class="text-xs whitespace-pre-wrap font-mono">%s</pre>`, formatted)
}