-
Notifications
You must be signed in to change notification settings - Fork 44
/
format.go
112 lines (89 loc) · 2.51 KB
/
format.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
// Copyright 2013 Chris McGee <sirnewton_01@yahoo.ca>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"bytes"
"net/http"
"os/exec"
"regexp"
"strconv"
)
func formatHandler(writer http.ResponseWriter, req *http.Request, path string, pathSegs []string) bool {
switch {
case req.Method == "GET":
qValues := req.URL.Query()
pkg := qValues.Get("pkg")
if pkg != "" {
cmd := exec.Command("go", "fmt", pkg)
err := cmd.Run()
if err != nil {
ShowError(writer, 400, "Go format ran with errors", err)
return true
}
writer.WriteHeader(200)
return true
}
case req.Method == "POST":
qValues := req.URL.Query()
showLines := qValues.Get("showLines")
// Simple case, provide the output from gofmt
if showLines != "true" {
cmd := exec.Command("gofmt")
cmd.Stdin = req.Body
output, err := cmd.Output()
if err != nil {
ShowError(writer, 500, "Error formatting go file", err)
return true
}
writer.WriteHeader(200)
writer.Write(output)
return true
} else {
// Get the specific line numbers where there are formatting problems
cmd := exec.Command("gofmt", "-d")
cmd.Stdin = req.Body
output, err := cmd.Output()
if err != nil {
ShowError(writer, 500, "Error formatting go file", err)
return true
}
if err != nil {
ShowError(writer, 500, "Unable to parse format diff", err)
return true
}
formatWarnings := []int64{}
headerRegx := regexp.MustCompile(`^@@ -(\d+),(\d+) \+(\d+),(\d+) @@$`)
firstLine := int64(-1)
lineCounter := int64(-1)
reader := bytes.NewReader(output)
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
hunk := scanner.Text()
match := headerRegx.FindStringSubmatch(hunk)
if len(match) > 1 {
n, err := strconv.ParseInt(match[1], 10, 64)
if err != nil {
ShowError(writer, 500, "Unable to parse format diff", err)
return true
}
firstLine = n
lineCounter = -1
}
// This is a line that has a diff, mark it down
if firstLine != -1 && hunk[0] == '-' {
formatWarnings = append(formatWarnings, firstLine+lineCounter)
}
// Don't count the adds for the format proposal
// TODO Some day we could provide the exact column within the original where the formatting warning exists
if firstLine != -1 && hunk[0] != '+' {
lineCounter++
}
}
ShowJson(writer, 200, formatWarnings)
return true
}
}
return false
}