2015-12-30 11:13:43 +03:00
|
|
|
// Copyright (c) 2014-2016, b3log.org & hacpai.com
|
2015-03-07 19:01:06 +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
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// 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 git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
|
2015-03-08 06:39:55 +03:00
|
|
|
"github.com/b3log/wide/conf"
|
|
|
|
"github.com/b3log/wide/i18n"
|
2015-03-07 19:01:06 +03:00
|
|
|
"github.com/b3log/wide/log"
|
|
|
|
"github.com/b3log/wide/session"
|
|
|
|
"github.com/b3log/wide/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Logger.
|
|
|
|
var logger = log.NewLogger(os.Stdout)
|
|
|
|
|
|
|
|
// Clone handles request of git clone.
|
|
|
|
func CloneHandler(w http.ResponseWriter, r *http.Request) {
|
2015-11-24 12:39:35 +03:00
|
|
|
result := util.NewResult()
|
|
|
|
defer util.RetResult(w, r, result)
|
2015-03-07 19:01:06 +03:00
|
|
|
|
|
|
|
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
|
|
|
|
if httpSession.IsNew {
|
|
|
|
http.Error(w, "Forbidden", http.StatusForbidden)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
username := httpSession.Values["username"].(string)
|
2015-03-08 06:00:56 +03:00
|
|
|
locale := conf.GetUser(username).Locale
|
2015-03-07 19:01:06 +03:00
|
|
|
|
|
|
|
var args map[string]interface{}
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
|
|
|
|
logger.Error(err)
|
2015-11-24 12:39:35 +03:00
|
|
|
result.Succ = false
|
2015-03-07 19:01:06 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sid := args["sid"].(string)
|
|
|
|
path := args["path"].(string)
|
|
|
|
repository := args["repository"].(string)
|
|
|
|
|
|
|
|
cmd := exec.Command("git", "clone", repository)
|
|
|
|
cmd.Dir = path
|
|
|
|
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if nil != err {
|
|
|
|
logger.Error(err)
|
2015-11-24 12:39:35 +03:00
|
|
|
result.Succ = false
|
2015-03-07 19:01:06 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stderr, err := cmd.StderrPipe()
|
|
|
|
if nil != err {
|
|
|
|
logger.Error(err)
|
2015-11-24 12:39:35 +03:00
|
|
|
result.Succ = false
|
2015-03-07 19:01:06 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-11-24 12:39:35 +03:00
|
|
|
if !result.Succ {
|
2015-03-07 19:01:06 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
channelRet := map[string]interface{}{}
|
|
|
|
|
|
|
|
if nil != session.OutputWS[sid] {
|
|
|
|
// display "START [git clone]" in front-end browser
|
|
|
|
|
2015-03-08 06:00:56 +03:00
|
|
|
channelRet["output"] = "<span class='start-get'>" + i18n.Get(locale, "start-git_clone").(string) + "</span>\n"
|
2015-03-07 19:01:06 +03:00
|
|
|
channelRet["cmd"] = "start-git_clone"
|
|
|
|
|
|
|
|
wsChannel := session.OutputWS[sid]
|
|
|
|
|
|
|
|
err := wsChannel.WriteJSON(&channelRet)
|
|
|
|
if nil != err {
|
2015-03-20 11:49:58 +03:00
|
|
|
logger.Warn(err)
|
2015-03-07 19:01:06 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
wsChannel.Refresh()
|
|
|
|
}
|
|
|
|
|
|
|
|
reader := bufio.NewReader(io.MultiReader(stdout, stderr))
|
|
|
|
|
|
|
|
if err := cmd.Start(); nil != err {
|
|
|
|
logger.Error(err)
|
2015-11-24 12:39:35 +03:00
|
|
|
result.Succ = false
|
2015-03-07 19:01:06 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func(runningId int) {
|
|
|
|
defer util.Recover()
|
|
|
|
defer cmd.Wait()
|
|
|
|
|
|
|
|
logger.Debugf("User [%s, %s] is running [git clone] [runningId=%d]", username, sid, runningId)
|
|
|
|
|
|
|
|
channelRet := map[string]interface{}{}
|
|
|
|
channelRet["cmd"] = "git clone"
|
|
|
|
|
|
|
|
// read all
|
2015-03-20 12:41:49 +03:00
|
|
|
buf, err := ioutil.ReadAll(reader)
|
|
|
|
if nil != err {
|
|
|
|
logger.Warn(err)
|
|
|
|
|
|
|
|
// TODO: handle clone error
|
|
|
|
}
|
2015-03-07 19:01:06 +03:00
|
|
|
|
|
|
|
logger.Debugf("User [%s, %s] 's running [git clone] [runningId=%d] has done: %s", username, sid, runningId, string(buf))
|
|
|
|
|
2015-03-08 06:00:56 +03:00
|
|
|
channelRet["output"] = "<span class='get-succ'>" + i18n.Get(locale, "git_clone-done").(string) + "</span>\n"
|
2015-03-07 19:01:06 +03:00
|
|
|
|
|
|
|
if nil != session.OutputWS[sid] {
|
|
|
|
wsChannel := session.OutputWS[sid]
|
|
|
|
|
|
|
|
err := wsChannel.WriteJSON(&channelRet)
|
|
|
|
if nil != err {
|
2015-03-20 11:49:58 +03:00
|
|
|
logger.Warn(err)
|
2015-03-07 19:01:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
wsChannel.Refresh()
|
|
|
|
}
|
|
|
|
}(rand.Int())
|
|
|
|
}
|