Files
MonkeyCode/backend/pkg/cli/cli.go

38 lines
1.0 KiB
Go
Raw Normal View History

2025-07-24 16:14:07 +08:00
package cli
import (
"encoding/json"
"os"
"os/exec"
"github.com/chaitin/MonkeyCode/backend/domain"
)
2025-07-25 17:54:38 +08:00
// RunCli
// @Tags WorkspaceFile
// @Description: 运行ctcode-cli命令
// @param command 命令
// @param flag [ -m | --maxlines <int> ] 解析时允许的最大 CodeSnippet 行数
// @param codeFiles 代码文件信息
// @return string 输出结果
// @return error
func RunCli(command string, flag string, codeFiles domain.CodeFiles) ([]domain.IndexResult, error) {
inputJson, err := json.Marshal(codeFiles)
2025-07-24 16:14:07 +08:00
if err != nil {
2025-07-25 17:54:38 +08:00
return []domain.IndexResult{}, err
2025-07-24 16:14:07 +08:00
}
2025-07-25 17:54:38 +08:00
cmd := exec.Command("ctcode-cli", command, flag, string(inputJson))
cmd.Env = os.Environ()
output, err := cmd.CombinedOutput()
if err != nil {
return []domain.IndexResult{}, err
2025-07-24 16:14:07 +08:00
}
2025-07-25 17:54:38 +08:00
var res []domain.IndexResult
err = json.Unmarshal(output, &res)
if err!= nil {
return []domain.IndexResult{}, err
}
return res, nil
2025-07-24 16:14:07 +08:00
}