Files
MonkeyCode/backend/pkg/cli/cli.go
chriscco a03d6647d5 fix: 修复了CLI传入字段的对齐问题
fix: Update GetAndSaveReq structure to use WorkspaceID and FileMetas fields

feat: Update GetAndSave request structure to include workspace_id and refactor related code
2025-07-28 16:20:57 +08:00

49 lines
1.2 KiB
Go

package cli
import (
"encoding/json"
"os"
"os/exec"
"github.com/chaitin/MonkeyCode/backend/domain"
)
// RunCli 运行monkeycode-cli命令
//
// @Tags CLI
// @Summary 运行monkeycode-cli命令
// @Description 运行monkeycode-cli命令
// @Accept json
// @Produce json
// @Param command path string true "命令"
// @Param flag query string false "标志"
// @Param codeFiles body domain.CodeFiles true "代码文件信息"
// @Success 200 {object} []domain.IndexResult "输出结果"
// @Failure 500 {object} web.Resp "内部错误"
// @Router /api/v1/cli/{command} [post]
func RunCli(command string, flag string, fileMetas []domain.FileMeta) ([]domain.IndexResult, error) {
inputJson, err := json.Marshal(fileMetas)
if err != nil {
return []domain.IndexResult{}, err
}
var cmd *exec.Cmd
if flag == "" {
cmd = exec.Command("monkeycode-cli", command, string(inputJson))
} else {
cmd = exec.Command("monkeycode-cli", command, flag, string(inputJson))
}
cmd.Env = os.Environ()
output, err := cmd.CombinedOutput()
if err != nil {
return []domain.IndexResult{}, err
}
var res []domain.IndexResult
err = json.Unmarshal(output, &res)
if err != nil {
return []domain.IndexResult{}, err
}
return res, nil
}