Files
MonkeyCode/backend/pkg/cli/cli.go
Haoxin Li 9388e149d6 feat: Add CodeSnippet entity and related functionality
- Introduced CodeSnippet schema with fields for snippet details and relationships to WorkspaceFile.
- Enhanced WorkspaceFileUpdate and WorkspaceFileUpdateOne to manage snippets, including adding, removing, and clearing snippet relationships.
- Updated Swagger documentation to include new API endpoints for CLI command execution and workspace file management.
- Implemented domain structures for handling code files and AST parsing results.
- Refactored Workspace and WorkspaceFile use cases to support new functionalities.
- Adjusted CLI command execution to handle code file information in JSON format.
- Improved error handling and logging throughout the workspace and file management processes.
2025-07-25 19:35:00 +08:00

42 lines
1.1 KiB
Go

package cli
import (
"encoding/json"
"os"
"os/exec"
"github.com/chaitin/MonkeyCode/backend/domain"
)
// RunCli 运行ctcode-cli命令
//
// @Tags CLI
// @Summary 运行ctcode-cli命令
// @Description 运行ctcode-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, codeFiles domain.CodeFiles) ([]domain.IndexResult, error) {
inputJson, err := json.Marshal(codeFiles)
if err != nil {
return []domain.IndexResult{}, err
}
cmd := exec.Command("ctcode-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
}