2019-05-17 06:28:50 +03:00
|
|
|
// Copyright (c) 2014-present, b3log.org
|
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"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2019-12-01 15:21:00 +03:00
|
|
|
"github.com/88250/gulu"
|
|
|
|
"github.com/88250/wide/conf"
|
|
|
|
"github.com/88250/wide/i18n"
|
|
|
|
"github.com/88250/wide/session"
|
2015-01-13 12:28:30 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// GoInstallHandler handles request of go install.
|
|
|
|
func GoInstallHandler(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)
|
2015-01-13 12:28:30 +03:00
|
|
|
|
2022-08-09 04:43:46 +03:00
|
|
|
if conf.Wide.ReadOnly {
|
|
|
|
result.Code = -1
|
|
|
|
result.Msg = "readonly mode"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-16 19:41:52 +03:00
|
|
|
httpSession, _ := session.HTTPSession.Get(r, session.CookieName)
|
2015-01-13 12:28:30 +03:00
|
|
|
if httpSession.IsNew {
|
|
|
|
http.Error(w, "Forbidden", http.StatusForbidden)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2019-05-16 18:17:25 +03:00
|
|
|
uid := httpSession.Values["uid"].(string)
|
|
|
|
locale := conf.GetUser(uid).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)
|
2019-05-24 16:52:17 +03:00
|
|
|
result.Code = -1
|
2015-01-13 12:28:30 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sid := args["sid"].(string)
|
|
|
|
|
|
|
|
filePath := args["file"].(string)
|
|
|
|
curDir := filepath.Dir(filePath)
|
|
|
|
|
|
|
|
cmd := exec.Command("go", "install")
|
|
|
|
cmd.Dir = curDir
|
|
|
|
|
2019-05-16 18:17:25 +03:00
|
|
|
setCmdEnv(cmd, uid)
|
2015-01-13 12:28:30 +03:00
|
|
|
|
|
|
|
logger.Debugf("go install %s", curDir)
|
|
|
|
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if nil != err {
|
|
|
|
logger.Error(err)
|
2019-05-24 16:52:17 +03:00
|
|
|
result.Code = -1
|
2015-01-13 12:28:30 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stderr, err := cmd.StderrPipe()
|
|
|
|
if nil != err {
|
|
|
|
logger.Error(err)
|
2019-05-24 16:52:17 +03:00
|
|
|
result.Code = -1
|
2015-01-13 12:28:30 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-24 16:52:17 +03:00
|
|
|
if 0 != result.Code {
|
2015-01-13 12:28:30 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
channelRet := map[string]interface{}{}
|
|
|
|
|
|
|
|
if nil != session.OutputWS[sid] {
|
|
|
|
// display "START [go install]" in front-end browser
|
|
|
|
|
|
|
|
channelRet["output"] = "<span class='start-install'>" + i18n.Get(locale, "start-install").(string) + "</span>\n"
|
|
|
|
channelRet["cmd"] = "start-install"
|
|
|
|
|
|
|
|
wsChannel := session.OutputWS[sid]
|
|
|
|
|
|
|
|
err := wsChannel.WriteJSON(&channelRet)
|
|
|
|
if nil != err {
|
|
|
|
logger.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
wsChannel.Refresh()
|
|
|
|
}
|
|
|
|
|
|
|
|
reader := bufio.NewReader(io.MultiReader(stdout, stderr))
|
|
|
|
|
|
|
|
if err := cmd.Start(); nil != err {
|
|
|
|
logger.Error(err)
|
2019-05-24 16:52:17 +03:00
|
|
|
result.Code = -1
|
2015-01-13 12:28:30 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func(runningId int) {
|
2019-08-06 07:04:27 +03:00
|
|
|
defer gulu.Panic.Recover(nil)
|
2015-01-13 12:28:30 +03:00
|
|
|
defer cmd.Wait()
|
|
|
|
|
2019-05-16 18:17:25 +03:00
|
|
|
logger.Debugf("User [%s, %s] is running [go install] [id=%d, dir=%s]", uid, sid, runningId, curDir)
|
2015-01-13 12:28:30 +03:00
|
|
|
|
|
|
|
// read all
|
|
|
|
buf, _ := ioutil.ReadAll(reader)
|
|
|
|
|
|
|
|
channelRet := map[string]interface{}{}
|
|
|
|
channelRet["cmd"] = "go install"
|
|
|
|
|
|
|
|
if 0 != len(buf) { // build error
|
|
|
|
// build gutter lint
|
|
|
|
|
|
|
|
errOut := string(buf)
|
|
|
|
lines := strings.Split(errOut, "\n")
|
|
|
|
|
|
|
|
if lines[0][0] == '#' {
|
|
|
|
lines = lines[1:] // skip the first line
|
|
|
|
}
|
|
|
|
|
|
|
|
lints := []*Lint{}
|
|
|
|
|
|
|
|
for _, line := range lines {
|
|
|
|
if len(line) < 1 {
|
|
|
|
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, _ = strconv.Atoi(left[:index])
|
|
|
|
msg = left[index+2:]
|
|
|
|
}
|
|
|
|
|
|
|
|
lint := &Lint{
|
|
|
|
File: file,
|
|
|
|
LineNo: lineNo - 1,
|
|
|
|
Severity: lintSeverityError,
|
|
|
|
Msg: msg,
|
|
|
|
}
|
|
|
|
|
|
|
|
lints = append(lints, lint)
|
|
|
|
}
|
|
|
|
|
|
|
|
channelRet["lints"] = lints
|
|
|
|
|
|
|
|
channelRet["output"] = "<span class='install-error'>" + i18n.Get(locale, "install-error").(string) + "</span>\n" + errOut
|
|
|
|
} else {
|
|
|
|
channelRet["output"] = "<span class='install-succ'>" + i18n.Get(locale, "install-succ").(string) + "</span>\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
if nil != session.OutputWS[sid] {
|
2019-05-16 18:17:25 +03:00
|
|
|
logger.Debugf("User [%s, %s] 's running [go install] [id=%d, dir=%s] has done", uid, sid, runningId, curDir)
|
2015-01-13 12:28:30 +03:00
|
|
|
|
|
|
|
wsChannel := session.OutputWS[sid]
|
|
|
|
err := wsChannel.WriteJSON(&channelRet)
|
|
|
|
if nil != err {
|
2015-03-20 11:49:58 +03:00
|
|
|
logger.Warn(err)
|
2015-01-13 12:28:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
wsChannel.Refresh()
|
|
|
|
}
|
|
|
|
|
|
|
|
}(rand.Int())
|
|
|
|
}
|