-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathimports.go
39 lines (31 loc) · 875 Bytes
/
imports.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
// Copyright 2013 Chris McGee <[email protected]>. 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 (
"net/http"
"os/exec"
"strings"
)
func importsHandler(writer http.ResponseWriter, req *http.Request, path string, pathSegs []string) bool {
switch {
case req.Method == "POST":
cmd := exec.Command("goimports")
cmd.Stdin = req.Body
output, err := cmd.Output()
if err != nil {
if strings.Contains(strings.ToLower(err.Error()), "not found") {
// Executable was not found, inform the user
ShowError(writer, 500, "Import tool failed or is not installed.", err)
return true
}
// Tool could not format the input
writer.WriteHeader(204)
return true
}
writer.WriteHeader(200)
writer.Write(output)
return true
}
return false
}