From fd71cc85c43640f576da019e92cc5fabc7ee8932 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Sat, 6 Jul 2024 19:06:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=84=9A=E6=9C=AC=E4=BB=93?= =?UTF-8?q?=E5=BA=93=E5=90=8D=E7=A7=B0=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/cid.go | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) 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"})