MCPcopy Create free account
hub / github.com/diillson/chatcli / editCommandBlock

Method editCommandBlock

cli/agent_command_blocks.go:986–1044  ·  view source on GitHub ↗

editCommandBlock abre comandos em editor

(block agent.CommandBlock)

Source from the content-addressed store, hash-verified

984
985// editCommandBlock abre comandos em editor
986func (a *AgentMode) editCommandBlock(block agent.CommandBlock) ([]string, error) {
987 choice := a.getInput(i18n.T("agent.status.edit_prompt"))
988 choice = strings.ToLower(strings.TrimSpace(choice))
989
990 if choice == "t" {
991 editedCommands := make([]string, len(block.Commands))
992
993 for i, cmd := range block.Commands {
994 if cmd == "" {
995 continue
996 }
997
998 prompt := fmt.Sprintf(i18n.T("agent.status.edit_command_prompt", i+1, len(block.Commands), block.Language), i+1, len(block.Commands), block.Language)
999 edited := a.getInput(prompt)
1000
1001 if edited == "" {
1002 edited = cmd
1003 }
1004
1005 editedCommands[i] = edited
1006 }
1007
1008 return editedCommands, nil
1009 }
1010
1011 editor, err := resolveEditor()
1012 if err != nil {
1013 return nil, fmt.Errorf("invalid editor configuration: %w", err)
1014 }
1015 tmpfile, err := os.CreateTemp("", "agent-edit-*.sh")
1016 if err != nil {
1017 return nil, err
1018 }
1019 defer func() { _ = os.Remove(tmpfile.Name()) }()
1020
1021 content := strings.Join(block.Commands, "\n")
1022 if _, err := tmpfile.Write([]byte(content)); err != nil {
1023 return nil, err
1024 }
1025 if err := tmpfile.Close(); err != nil {
1026 return nil, err
1027 }
1028
1029 cmd := exec.Command(editor, tmpfile.Name()) //#nosec G204 -- agent/CLI tool execution; commands validated by command_validator + policy_manager upstream
1030 cmd.Stdin = os.Stdin
1031 cmd.Stdout = os.Stdout
1032 cmd.Stderr = os.Stderr
1033 if err := cmd.Run(); err != nil {
1034 return nil, err
1035 }
1036
1037 edited, err := os.ReadFile(tmpfile.Name())
1038 if err != nil {
1039 return nil, err
1040 }
1041
1042 lines := strings.Split(strings.ReplaceAll(string(edited), "\r\n", "\n"), "\n")
1043 return lines, nil

Callers 1

cmdEditAndExecuteMethod · 0.95

Calls 9

getInputMethod · 0.95
TFunction · 0.92
resolveEditorFunction · 0.85
ErrorfMethod · 0.80
RemoveMethod · 0.65
NameMethod · 0.65
WriteMethod · 0.65
CloseMethod · 0.65
RunMethod · 0.65

Tested by

no test coverage detected