wide/editor/formatter.go

107 lines
2.2 KiB
Go
Raw Normal View History

2015-01-18 08:59:10 +03:00
// Copyright (c) 2014-2015, b3log.org
2014-11-13 17:28:28 +03:00
//
2014-11-12 18:13:14 +03:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-11-13 17:28:28 +03:00
//
2014-11-12 18:13:14 +03:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-11-13 17:28:28 +03:00
//
2014-11-12 18:13:14 +03:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2014-09-07 13:07:25 +04:00
package editor
import (
"encoding/json"
"net/http"
"os"
2014-09-07 14:13:55 +04:00
"os/exec"
2014-09-07 13:07:25 +04:00
2014-10-26 11:03:19 +03:00
"github.com/b3log/wide/conf"
2014-10-26 13:08:01 +03:00
"github.com/b3log/wide/session"
2014-09-07 13:07:25 +04:00
"github.com/b3log/wide/util"
)
2014-10-29 13:15:18 +03:00
// GoFmtHandler handles request of formatting Go source code.
//
// This function will select a format tooll based on user's configuration:
2014-10-26 13:08:01 +03:00
// 1. gofmt
// 2. goimports
2014-09-07 14:13:55 +04:00
func GoFmtHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
2014-10-26 13:08:01 +03:00
session, _ := session.HTTPSession.Get(r, "wide-session")
2014-11-26 08:49:27 +03:00
if session.IsNew {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
2014-10-26 13:08:01 +03:00
username := session.Values["username"].(string)
2014-09-07 14:13:55 +04:00
var args map[string]interface{}
2014-10-26 13:08:01 +03:00
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-09-07 14:13:55 +04:00
data["succ"] = false
return
}
filePath := args["file"].(string)
2014-11-21 12:41:51 +03:00
if util.Go.IsAPI(filePath) {
2014-10-29 13:15:18 +03:00
// ignore it
2014-10-26 11:03:19 +03:00
return
}
2014-09-07 14:13:55 +04:00
fout, err := os.Create(filePath)
if nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-09-07 14:13:55 +04:00
data["succ"] = false
return
}
code := args["code"].(string)
fout.WriteString(code)
if err := fout.Close(); nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-09-07 14:13:55 +04:00
data["succ"] = false
return
}
fmt := conf.GetGoFmt(username)
2014-10-26 13:08:01 +03:00
2014-09-07 14:13:55 +04:00
argv := []string{filePath}
2014-10-26 13:08:01 +03:00
cmd := exec.Command(fmt, argv...)
2014-09-07 14:13:55 +04:00
bytes, _ := cmd.Output()
output := string(bytes)
if "" == output {
2014-11-13 17:28:28 +03:00
// format error, returns the original content
data["succ"] = true
data["code"] = code
2014-09-07 14:13:55 +04:00
return
}
code = string(output)
data["code"] = code
fout, err = os.Create(filePath)
fout.WriteString(code)
if err := fout.Close(); nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-09-07 14:13:55 +04:00
data["succ"] = false
return
}
}