This commit is contained in:
Liang Ding 2014-10-26 18:08:01 +08:00
parent ad75e1088b
commit a377ff5ae0
3 changed files with 29 additions and 10 deletions

View File

@ -36,6 +36,7 @@ type User struct {
Password string
Workspace string // 该用户的工作空间 GOPATH 路径
Locale string
GoFormat string
LatestSessionContent *LatestSessionContent
}
@ -131,11 +132,23 @@ func (c *conf) GetWorkspace() string {
return filepath.FromSlash(strings.Replace(c.Workspace, "{pwd}", c.Pwd, 1))
}
// 获取 user 的工作空间路径.
func (user *User) getWorkspace() string {
ret := strings.Replace(user.Workspace, "{pwd}", Wide.Pwd, 1)
// 获取 username 指定的用户的 Go 源码格式化工具路径,查找不到时返回 "gofmt".
func (c *conf) GetGoFmt(username string) string {
for _, user := range c.Users {
if user.Name == username {
switch user.GoFormat {
case "gofmt":
return "gofmt"
case "goimports":
return c.GetExecutableInGOBIN("goimports")
default:
glog.Errorf("Unsupported Go Format tool [%s]", user.GoFormat)
return "gofmt"
}
}
}
return filepath.FromSlash(ret)
return "gofmt"
}
// 获取 username 指定的用户配置.

View File

@ -18,6 +18,7 @@
"Password": "admin",
"Workspace": "{pwd}/data/user_workspaces/admin",
"Locale": "zh_CN",
"GoFormat": "gofmt",
"LatestSessionContent": {
"FileTree": [],
"Files": [],

View File

@ -10,22 +10,25 @@ import (
"github.com/88250/gohtml"
"github.com/b3log/wide/conf"
"github.com/b3log/wide/session"
"github.com/b3log/wide/util"
"github.com/golang/glog"
)
// TODO: 加入 goimports 格式化 Go 源码文件
// gofmt 格式化 Go 源码文件.
// 格式化 Go 源码文件.
// 根据用户的 GoFormat 配置选择格式化工具:
// 1. gofmt
// 2. goimports
func GoFmtHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
decoder := json.NewDecoder(r.Body)
session, _ := session.HTTPSession.Get(r, "wide-session")
username := session.Values["username"].(string)
var args map[string]interface{}
if err := decoder.Decode(&args); err != nil {
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
glog.Error(err)
data["succ"] = false
@ -59,8 +62,10 @@ func GoFmtHandler(w http.ResponseWriter, r *http.Request) {
return
}
fmt := conf.Wide.GetGoFmt(username)
argv := []string{filePath}
cmd := exec.Command("gofmt", argv...)
cmd := exec.Command(fmt, argv...)
bytes, _ := cmd.Output()
output := string(bytes)