diff --git a/handler/cid.go b/handler/cid.go index dad05de..56c04fe 100644 --- a/handler/cid.go +++ b/handler/cid.go @@ -5,8 +5,8 @@ import ( "fmt" "github.com/gin-gonic/gin" "os/exec" - "regexp" "strconv" + "strings" "videoplayer/dao" "videoplayer/proto" ) @@ -55,9 +55,11 @@ func RunCID(c *gin.Context) { c.JSON(200, gin.H{"error": "CID not found", "code": proto.OperationFailed, "message": "failed"}) return } else { - re := regexp.MustCompile(`(?i)(?:https?://|git@)[^/]+/([^/]+)/([^/]+)(?:\.git)?$`) - matches := re.FindStringSubmatch(cid.Url) - name := matches[2] + strs := strings.Split(cid.Url, "/") + name := strs[len(strs)-1] + names := strings.Split(name, ".") + name = names[0] + //脚本内容 scriptContent := `#!/bin/bash TARGET_DIR = ` + proto.CID_BASE_DIR + `/workspace` + name + ` @@ -72,11 +74,15 @@ func RunCID(c *gin.Context) { //执行脚本 cmd := exec.Command("/bin/bash", "-c", scriptContent) err3 := cmd.Run() + err3_info := "" + if err3 != nil { + err3_info = err3.Error() + } fmt.Println("bash content:", scriptContent) // 使用bytes.Buffer捕获输出 var out bytes.Buffer cmd.Stdout = &out - dao.CreateRunLog(req.ID, authID, scriptContent, out.String(), err3.Error()) //添加执行日志 + dao.CreateRunLog(req.ID, authID, scriptContent, out.String(), err3_info) //添加执行日志 c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"}) } @@ -183,9 +189,10 @@ func CIDCallback(c *gin.Context) { res := dao.FindCIDByIDAndToken(cid, token) if res.ID != 0 { //从url获取仓库名称 - re := regexp.MustCompile(`(?i)(?:https?://|git@)[^/]+/([^/]+)/([^/]+)(?:\.git)?$`) - matches := re.FindStringSubmatch(res.Url) - name := matches[2] + strs := strings.Split(res.Url, "/") + name := strs[len(strs)-1] + names := strings.Split(name, ".") + name = names[0] scriptContent := `#!/bin/bash TARGET_DIR = ` + proto.CID_BASE_DIR + `/workspace` + name + ` if [ ! -d $TARGET_DIR ]; then @@ -199,10 +206,14 @@ func CIDCallback(c *gin.Context) { //执行脚本 cmd := exec.Command("/bin/bash", "-c", scriptContent) err3 := cmd.Run() + err3_info := "" + if err3 != nil { + err3_info = err3.Error() + } // 使用bytes.Buffer捕获输出 var out bytes.Buffer cmd.Stdout = &out - dao.CreateRunLog(cid, res.Auth_id, scriptContent, out.String(), err3.Error()) //添加执行日志 + dao.CreateRunLog(cid, res.Auth_id, scriptContent, out.String(), err3_info) //添加执行日志 c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"}) } else { c.JSON(200, gin.H{"error": "CID not found by id and token", "code": proto.OperationFailed, "message": "failed"})