-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
330 lines (296 loc) · 7.34 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
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
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/karrick/godirwalk"
"gopkg.in/alecthomas/kingpin.v2"
)
const (
portHelp = "TCP port blackd listens on."
diffHelp = "Don't write the files back, just output a diff for each file on stdout."
checkHelp = "Don't write the files back, just return the status. " +
"Return code 0 means nothing would change. Return code 1 means some files would be reformatted. " +
"Return code 123 means there was an internal error."
)
var (
ports = kingpin.Flag("port", portHelp).Required().Uint16List()
diff = kingpin.Flag("diff", diffHelp).Bool()
check = kingpin.Flag("check", checkHelp).Bool()
files = kingpin.Arg("files", "Files to format").Strings()
)
type Action int
const (
Unchanged Action = iota
Reformatted
WouldBeReformatted
Error
)
func infof(format string, v ...interface{}) {
_, _ = fmt.Fprintf(os.Stderr, format, v...)
}
func main() {
log.SetFlags(0)
kingpin.Parse()
pathQueue := make(chan string, len(*ports))
actQueue := make(chan Action, 99)
wg := sync.WaitGroup{}
for _, port := range *ports {
wg.Add(1)
go func(port string) {
conf := BlackConfig{
Url: fmt.Sprintf("http://127.0.0.1:%s", port),
Check: *check,
Diff: *diff,
}
for path := range pathQueue {
actQueue <- processPath(conf, path)
}
wg.Done()
}(strconv.FormatUint(uint64(port), 10))
}
go func() {
wg.Wait()
close(actQueue)
}()
go walkDirectories(*files, pathQueue)
os.Exit(report(*check, actQueue))
}
func walkDirectories(paths []string, pathQueue chan<- string) {
for _, path := range paths {
err := godirwalk.Walk(path, &godirwalk.Options{
FollowSymbolicLinks: true,
Unsorted: true,
AllowNonDirectory: true,
Callback: func(path string, de *godirwalk.Dirent) error {
if de.IsRegular() && strings.HasSuffix(path, ".py") {
pathQueue <- path
}
return nil
},
ErrorCallback: func(path string, err error) godirwalk.ErrorAction {
infof("cannot format %s: %v\n", path, err)
return godirwalk.SkipNode
},
})
if err != nil {
log.Fatalf("error traversing %s: %v", path, err)
}
}
close(pathQueue)
}
func report(check bool, actQueue <-chan Action) int {
exitCode := 0
unchangedCount := 0
reformattedCount := 0
errorCount := 0
for act := range actQueue {
switch act {
case Unchanged:
unchangedCount += 1
case Reformatted:
reformattedCount += 1
case WouldBeReformatted:
reformattedCount += 1
if exitCode < 1 {
exitCode = 1
}
case Error:
errorCount += 1
exitCode = 123
}
}
if unchangedCount == 0 && reformattedCount == 0 && errorCount == 0 {
fmt.Println("No Python files are present to be formatted. Nothing to do 😴")
return exitCode
}
b := strings.Builder{}
if reformattedCount > 0 {
reportCount(&b, check, reformattedCount, "would be reformatted", "reformatted")
}
if unchangedCount > 0 {
reportCount(&b, check, unchangedCount, "would be left unchanged", "left unchanged")
}
if errorCount > 0 {
reportCount(&b, check, errorCount, "would fail to reformat", "failed to reformat")
}
b.WriteRune('.')
log.Println(b.String())
return exitCode
}
func reportCount(buf *strings.Builder, check bool, count int, statusWithCheck, statusWithoutCheck string) {
buf.Grow(10 + len(statusWithCheck))
if buf.Len() > 0 {
buf.WriteString(", ")
}
buf.WriteString(strconv.Itoa(count))
buf.WriteString(" file")
if count > 1 {
buf.WriteString("s ")
} else {
buf.WriteRune(' ')
}
if check {
buf.WriteString(statusWithCheck)
} else {
buf.WriteString(statusWithoutCheck)
}
}
func processPath(conf BlackConfig, path string) Action {
resp, err := queryBlackd(conf, path)
if err != nil {
infof("error: cannot format %s: %v\n", path, err)
return Error
}
defer resp.Body.Close()
defer io.Copy(ioutil.Discard, resp.Body)
res, blackErr := newBlackResult(resp)
if blackErr != nil {
if blackErr.Syntax {
infof("%s: %s\n", path, blackErr.Msg)
} else {
infof("cannot format %s: %s\n", path, blackErr.Msg)
}
return Error
}
if !res.Changed {
return Unchanged
}
if conf.Diff && !printDiff(path, res.Text) {
return Error
}
if conf.Check {
infof("would reformat %s\n", path)
return WouldBeReformatted
}
if err = overwritePath(path, res.Text); err != nil {
log.Print(err)
return Error
}
infof("reformatted %s\n", path)
return Reformatted
}
func printDiff(path string, diff io.Reader) bool {
buf := bufio.NewReader(diff)
ok := printDiffHeader(path, "In", buf)
ok = ok && printDiffHeader(path, "Out", buf)
if !ok {
infof("%s: internal error: blackd returned an invalid diff\n", path)
return false
}
_, err := io.Copy(os.Stdout, buf)
if err != nil {
log.Print(err)
return false
}
return true
}
func printDiffHeader(path string, oldPath string, buf *bufio.Reader) bool {
header, err := buf.ReadString('\n')
if err != nil {
return false
}
header = strings.Replace(header, oldPath, path, 1)
_, _ = os.Stdout.WriteString(header)
return true
}
func newHttpClient() *http.Client {
// http://tleyden.github.io/blog/2016/11/21/tuning-the-go-http-client-library-for-load-testing/
// Customize the Transport to have a larger connection pool.
defaultRoundTripper := http.DefaultTransport
defaultTransportPointer, ok := defaultRoundTripper.(*http.Transport)
if !ok {
panic(fmt.Sprintf("defaultRoundTripper is not an *http.Transport"))
}
// Dereference to get a copy of the struct that the pointer points to.
defaultTransport := *defaultTransportPointer
defaultTransport.MaxIdleConns = 100
defaultTransport.MaxIdleConnsPerHost = 100
return &http.Client{
Transport: &defaultTransport,
Timeout: 5 * time.Second,
}
}
var (
client = newHttpClient()
)
func queryBlackd(conf BlackConfig, path string) (*http.Response, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
r := bufio.NewReader(file)
req, err := http.NewRequest("POST", conf.Url, r)
if err != nil {
return nil, fmt.Errorf("%s: %v", path, err)
}
if conf.Diff {
req.Header.Set("X-Diff", "1")
}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("%s: couldn't reach blackd: %v", path, err)
}
return resp, nil
}
func newBlackResult(resp *http.Response) (*BlackResult, *BlackError) {
switch resp.StatusCode {
case 204:
return &BlackResult{}, nil
case 200:
return &BlackResult{Changed: true, Text: resp.Body}, nil
case 400:
return nil, &BlackError{Syntax: true, Msg: newStringFromReader(resp.Body)}
case 500:
return nil, &BlackError{Msg: newStringFromReader(resp.Body)}
}
log.Fatalf("unsupported HTTP status: %v", resp.StatusCode)
return nil, nil // never reached
}
func overwritePath(path string, newContents io.Reader) error {
file, err := os.Create(path)
if err != nil {
return fmt.Errorf("%s: cannot format: %v", path, err)
}
defer file.Close()
w := bufio.NewWriter(file)
_, err = io.Copy(w, newContents)
if err == nil {
err = file.Sync()
}
if err != nil {
return fmt.Errorf("%s: formatting failed: %v", path, err)
}
return nil
}
type BlackConfig struct {
Url string
Diff bool
Check bool
}
type BlackResult struct {
Changed bool
Text io.Reader
}
type BlackError struct {
Syntax bool
Msg string
}
func newStringFromReader(r io.Reader) string {
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(r)
if err != nil {
panic(err)
}
return buf.String()
}