2019-01-02 16:43:11 +03:00
|
|
|
// Copyright (c) 2014-2019, b3log.org & hacpai.com
|
2015-01-13 12:28:30 +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
|
|
|
|
//
|
2018-03-12 07:28:33 +03:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2015-01-13 12:28:30 +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.
|
|
|
|
|
|
|
|
package output
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"encoding/json"
|
2017-03-27 05:04:34 +03:00
|
|
|
"fmt"
|
2015-01-13 12:28:30 +03:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2017-03-27 16:31:35 +03:00
|
|
|
"runtime"
|
2017-03-15 18:26:10 +03:00
|
|
|
"strconv"
|
2015-01-13 12:28:30 +03:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/b3log/wide/conf"
|
|
|
|
"github.com/b3log/wide/i18n"
|
|
|
|
"github.com/b3log/wide/session"
|
|
|
|
"github.com/b3log/wide/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// BuildHandler handles request of building.
|
|
|
|
func BuildHandler(w http.ResponseWriter, r *http.Request) {
|
2015-11-24 11:30:37 +03:00
|
|
|
result := util.NewResult()
|
|
|
|
defer util.RetResult(w, r, result)
|
2015-01-13 12:28:30 +03:00
|
|
|
|
|
|
|
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
|
|
|
|
if httpSession.IsNew {
|
|
|
|
http.Error(w, "Forbidden", http.StatusForbidden)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
username := httpSession.Values["username"].(string)
|
2017-03-14 19:21:13 +03:00
|
|
|
user := conf.GetUser(username)
|
|
|
|
locale := user.Locale
|
2015-01-13 12:28:30 +03:00
|
|
|
|
|
|
|
var args map[string]interface{}
|
|
|
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
|
|
|
|
logger.Error(err)
|
2015-11-24 11:30:37 +03:00
|
|
|
result.Succ = false
|
2015-01-13 12:28:30 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sid := args["sid"].(string)
|
|
|
|
|
|
|
|
filePath := args["file"].(string)
|
2015-07-23 11:31:37 +03:00
|
|
|
|
|
|
|
if util.Go.IsAPI(filePath) || !session.CanAccess(username, filePath) {
|
|
|
|
http.Error(w, "Forbidden", http.StatusForbidden)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-13 12:28:30 +03:00
|
|
|
curDir := filepath.Dir(filePath)
|
|
|
|
|
|
|
|
fout, err := os.Create(filePath)
|
|
|
|
|
|
|
|
if nil != err {
|
|
|
|
logger.Error(err)
|
2015-11-24 11:30:37 +03:00
|
|
|
result.Succ = false
|
2015-01-13 12:28:30 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
code := args["code"].(string)
|
|
|
|
|
|
|
|
fout.WriteString(code)
|
|
|
|
|
|
|
|
if err := fout.Close(); nil != err {
|
|
|
|
logger.Error(err)
|
2015-11-24 11:30:37 +03:00
|
|
|
result.Succ = false
|
2015-01-13 12:28:30 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
suffix := ""
|
|
|
|
if util.OS.IsWindows() {
|
|
|
|
suffix = ".exe"
|
|
|
|
}
|
|
|
|
|
2017-03-14 19:21:13 +03:00
|
|
|
goBuildArgs := []string{}
|
2019-05-16 05:53:03 +03:00
|
|
|
goBuildArgs = append(goBuildArgs, "build", "-i")
|
2017-05-05 08:35:09 +03:00
|
|
|
goBuildArgs = append(goBuildArgs, user.BuildArgs(runtime.GOOS)...)
|
2017-03-14 19:21:13 +03:00
|
|
|
|
|
|
|
cmd := exec.Command("go", goBuildArgs...)
|
2015-01-13 12:28:30 +03:00
|
|
|
cmd.Dir = curDir
|
|
|
|
|
|
|
|
setCmdEnv(cmd, username)
|
|
|
|
|
|
|
|
executable := filepath.Base(curDir) + suffix
|
|
|
|
executable = filepath.Join(curDir, executable)
|
|
|
|
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if nil != err {
|
|
|
|
logger.Error(err)
|
2015-11-24 11:30:37 +03:00
|
|
|
result.Succ = false
|
2015-01-13 12:28:30 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stderr, err := cmd.StderrPipe()
|
|
|
|
if nil != err {
|
|
|
|
logger.Error(err)
|
2015-11-24 11:30:37 +03:00
|
|
|
result.Succ = false
|
2015-01-13 12:28:30 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-11-24 11:30:37 +03:00
|
|
|
if !result.Succ {
|
2015-01-13 12:28:30 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
channelRet := map[string]interface{}{}
|
|
|
|
|
|
|
|
if nil != session.OutputWS[sid] {
|
|
|
|
// display "START [go build]" in front-end browser
|
|
|
|
|
2017-03-14 19:21:13 +03:00
|
|
|
msg := i18n.Get(locale, "start-build").(string)
|
2017-05-05 08:35:09 +03:00
|
|
|
msg = strings.Replace(msg, "build]", "build "+fmt.Sprint(user.BuildArgs(runtime.GOOS))+"]", 1)
|
2017-03-14 19:21:13 +03:00
|
|
|
|
|
|
|
channelRet["output"] = "<span class='start-build'>" + msg + "</span>\n"
|
2015-01-13 12:28:30 +03:00
|
|
|
channelRet["cmd"] = "start-build"
|
|
|
|
|
|
|
|
wsChannel := session.OutputWS[sid]
|
|
|
|
|
|
|
|
err := wsChannel.WriteJSON(&channelRet)
|
|
|
|
if nil != err {
|
|
|
|
logger.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
wsChannel.Refresh()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cmd.Start(); nil != err {
|
|
|
|
logger.Error(err)
|
2015-11-24 11:30:37 +03:00
|
|
|
result.Succ = false
|
2015-01-13 12:28:30 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-15 17:26:12 +03:00
|
|
|
// logger.Debugf("User [%s, %s] is building [id=%d, dir=%s]", username, sid, runningId, curDir)
|
|
|
|
|
|
|
|
channelRet["cmd"] = "build"
|
|
|
|
channelRet["executable"] = executable
|
|
|
|
|
|
|
|
outReader := bufio.NewReader(stdout)
|
|
|
|
|
|
|
|
/////////
|
|
|
|
go func() {
|
2015-01-13 12:28:30 +03:00
|
|
|
defer util.Recover()
|
|
|
|
|
2017-03-15 17:26:12 +03:00
|
|
|
for {
|
|
|
|
wsChannel := session.OutputWS[sid]
|
|
|
|
if nil == wsChannel {
|
|
|
|
break
|
|
|
|
}
|
2015-01-13 12:28:30 +03:00
|
|
|
|
2017-03-15 17:26:12 +03:00
|
|
|
line, err := outReader.ReadString('\n')
|
|
|
|
if io.EOF == err {
|
|
|
|
break
|
|
|
|
}
|
2015-01-13 12:28:30 +03:00
|
|
|
|
2017-03-15 17:26:12 +03:00
|
|
|
if nil != err {
|
|
|
|
logger.Warn(err)
|
2015-01-13 12:28:30 +03:00
|
|
|
|
2017-03-15 17:26:12 +03:00
|
|
|
break
|
|
|
|
}
|
2017-03-14 19:21:13 +03:00
|
|
|
|
2017-03-15 17:26:12 +03:00
|
|
|
channelRet["output"] = line
|
2015-01-13 12:28:30 +03:00
|
|
|
|
2017-03-15 17:26:12 +03:00
|
|
|
err = wsChannel.WriteJSON(&channelRet)
|
|
|
|
if nil != err {
|
|
|
|
logger.Warn(err)
|
2019-05-16 13:10:44 +03:00
|
|
|
|
2017-03-15 17:26:12 +03:00
|
|
|
break
|
|
|
|
}
|
2015-03-16 06:24:55 +03:00
|
|
|
|
2017-03-15 17:26:12 +03:00
|
|
|
wsChannel.Refresh()
|
|
|
|
}
|
|
|
|
}()
|
2015-01-13 12:28:30 +03:00
|
|
|
|
2017-03-15 17:26:12 +03:00
|
|
|
errReader := bufio.NewReader(stderr)
|
2019-05-16 05:53:03 +03:00
|
|
|
var lines []string
|
2017-03-15 17:26:12 +03:00
|
|
|
for {
|
|
|
|
wsChannel := session.OutputWS[sid]
|
|
|
|
if nil == wsChannel {
|
|
|
|
break
|
|
|
|
}
|
2015-01-13 12:28:30 +03:00
|
|
|
|
2017-03-15 17:26:12 +03:00
|
|
|
line, err := errReader.ReadString('\n')
|
|
|
|
if io.EOF == err {
|
|
|
|
break
|
|
|
|
}
|
2015-01-13 12:28:30 +03:00
|
|
|
|
2017-03-15 18:26:10 +03:00
|
|
|
lines = append(lines, line)
|
|
|
|
|
2017-03-15 17:26:12 +03:00
|
|
|
if nil != err {
|
|
|
|
logger.Warn(err)
|
2015-01-13 12:28:30 +03:00
|
|
|
|
2017-03-15 17:26:12 +03:00
|
|
|
break
|
|
|
|
}
|
2015-01-13 12:28:30 +03:00
|
|
|
|
2017-03-15 17:26:12 +03:00
|
|
|
// path process
|
|
|
|
errOutWithPath := parsePath(curDir, line)
|
|
|
|
channelRet["output"] = "<span class='stderr'>" + errOutWithPath + "</span>"
|
2015-01-13 12:28:30 +03:00
|
|
|
|
2017-03-15 17:26:12 +03:00
|
|
|
err = wsChannel.WriteJSON(&channelRet)
|
|
|
|
if nil != err {
|
|
|
|
logger.Warn(err)
|
|
|
|
break
|
2015-01-13 12:28:30 +03:00
|
|
|
}
|
|
|
|
|
2017-03-15 17:26:12 +03:00
|
|
|
wsChannel.Refresh()
|
|
|
|
}
|
2015-01-13 12:28:30 +03:00
|
|
|
|
2017-03-15 17:26:12 +03:00
|
|
|
if nil == cmd.Wait() {
|
|
|
|
channelRet["nextCmd"] = args["nextCmd"]
|
|
|
|
channelRet["output"] = "<span class='build-succ'>" + i18n.Get(locale, "build-succ").(string) + "</span>\n"
|
|
|
|
} else {
|
|
|
|
channelRet["output"] = "<span class='build-error'>" + i18n.Get(locale, "build-error").(string) + "</span>\n"
|
2017-03-15 18:26:10 +03:00
|
|
|
|
|
|
|
// lint process
|
|
|
|
if lines[0][0] == '#' {
|
|
|
|
lines = lines[1:] // skip the first line
|
|
|
|
}
|
|
|
|
|
|
|
|
lints := []*Lint{}
|
|
|
|
|
|
|
|
for _, line := range lines {
|
|
|
|
if len(line) < 1 || !strings.Contains(line, ":") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if line[0] == '\t' {
|
|
|
|
// append to the last lint
|
|
|
|
last := len(lints)
|
|
|
|
msg := lints[last-1].Msg
|
|
|
|
msg += line
|
|
|
|
|
|
|
|
lints[last-1].Msg = msg
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
file := line[:strings.Index(line, ":")]
|
|
|
|
left := line[strings.Index(line, ":")+1:]
|
|
|
|
index := strings.Index(left, ":")
|
|
|
|
lineNo := 0
|
|
|
|
msg := left
|
|
|
|
if index >= 0 {
|
|
|
|
lineNo, err = strconv.Atoi(left[:index])
|
|
|
|
|
|
|
|
if nil != err {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = left[index+2:]
|
|
|
|
}
|
|
|
|
|
|
|
|
lint := &Lint{
|
|
|
|
File: filepath.ToSlash(filepath.Join(curDir, file)),
|
|
|
|
LineNo: lineNo - 1,
|
|
|
|
Severity: lintSeverityError,
|
|
|
|
Msg: msg,
|
|
|
|
}
|
|
|
|
|
|
|
|
lints = append(lints, lint)
|
|
|
|
}
|
|
|
|
|
|
|
|
channelRet["lints"] = lints
|
2017-03-15 17:26:12 +03:00
|
|
|
}
|
2015-01-13 12:28:30 +03:00
|
|
|
|
2017-03-15 17:26:12 +03:00
|
|
|
wsChannel := session.OutputWS[sid]
|
|
|
|
if nil == wsChannel {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = wsChannel.WriteJSON(&channelRet)
|
|
|
|
if nil != err {
|
|
|
|
logger.Warn(err)
|
|
|
|
}
|
2015-01-13 12:28:30 +03:00
|
|
|
|
2017-03-15 17:26:12 +03:00
|
|
|
wsChannel.Refresh()
|
2015-01-13 12:28:30 +03:00
|
|
|
}
|