wide/editor/formatter.go

120 lines
2.3 KiB
Go
Raw Normal View History

2019-05-17 06:28:50 +03:00
// Copyright (c) 2014-present, 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
//
2018-03-12 07:28:33 +03:00
// https://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
2019-12-01 15:21:00 +03:00
"github.com/88250/gulu"
"github.com/88250/wide/conf"
"github.com/88250/wide/session"
2014-09-07 13:07:25 +04:00
)
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) {
2019-05-24 16:04:25 +03:00
result := gulu.Ret.NewResult()
defer gulu.Ret.RetResult(w, r, result)
2014-09-07 14:13:55 +04:00
2022-08-09 05:01:55 +03:00
if conf.Wide.ReadOnly {
result.Code = -1
result.Msg = "readonly mode"
return
}
2019-05-16 19:41:52 +03:00
session, _ := session.HTTPSession.Get(r, session.CookieName)
2014-11-26 08:49:27 +03:00
if session.IsNew {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
2019-05-16 18:37:04 +03:00
uid := session.Values["uid"].(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)
2019-05-24 16:52:17 +03:00
result.Code = -1
2014-09-07 14:13:55 +04:00
return
}
filePath := args["file"].(string)
2019-05-24 16:04:25 +03:00
if gulu.Go.IsAPI(filePath) {
2019-05-24 16:52:17 +03:00
result.Code = -1
2015-07-23 11:31:37 +03:00
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)
2019-05-24 16:52:17 +03:00
result.Code = -1
2014-09-07 14:13:55 +04:00
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)
2019-05-24 16:52:17 +03:00
result.Code = -1
2014-09-07 14:13:55 +04:00
return
}
2015-11-24 12:39:35 +03:00
data := map[string]interface{}{}
2015-11-27 11:55:28 +03:00
result.Data = &data
2015-11-24 12:39:35 +03:00
data["code"] = code
result.Data = data
2019-05-16 18:37:04 +03:00
fmt := conf.GetGoFmt(uid)
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
2019-05-24 16:52:17 +03:00
result.Code = 0
2014-09-07 14:13:55 +04:00
return
}
code = string(output)
2015-11-24 12:39:35 +03:00
data["code"] = code
2014-09-07 14:13:55 +04:00
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)
2019-05-24 16:52:17 +03:00
result.Code = -1
2014-09-07 14:13:55 +04:00
return
}
}