|
| 1 | +package readline |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "runtime" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + defaultEditor = "vi" |
| 14 | + defaultShell = "/bin/sh" |
| 15 | + windowsEditor = "notepad" |
| 16 | + windowsShell = "cmd" |
| 17 | +) |
| 18 | + |
| 19 | +func openInEditor(fd uintptr, termios any, content string) (string, error) { |
| 20 | + if err := UnsetRawMode(fd, termios); err != nil { |
| 21 | + return content, err |
| 22 | + } |
| 23 | + |
| 24 | + edited, err := runEditor(content) |
| 25 | + |
| 26 | + if _, restoreErr := SetRawMode(fd); restoreErr != nil { |
| 27 | + return content, errors.Join(err, restoreErr) |
| 28 | + } |
| 29 | + |
| 30 | + if err != nil { |
| 31 | + return content, err |
| 32 | + } |
| 33 | + |
| 34 | + return edited, nil |
| 35 | +} |
| 36 | + |
| 37 | +func platformize(linux, windows string) string { |
| 38 | + if runtime.GOOS == "windows" { |
| 39 | + return windows |
| 40 | + } |
| 41 | + return linux |
| 42 | +} |
| 43 | + |
| 44 | +func defaultEnvShell() []string { |
| 45 | + shell := os.Getenv("SHELL") |
| 46 | + if shell == "" { |
| 47 | + shell = platformize(defaultShell, windowsShell) |
| 48 | + } |
| 49 | + flag := "-c" |
| 50 | + if shell == windowsShell { |
| 51 | + flag = "/C" |
| 52 | + } |
| 53 | + return []string{shell, flag} |
| 54 | +} |
| 55 | + |
| 56 | +func resolveEditor() ([]string, bool) { |
| 57 | + editor := strings.TrimSpace(os.Getenv("EDITOR")) |
| 58 | + if editor == "" { |
| 59 | + editor = platformize(defaultEditor, windowsEditor) |
| 60 | + } |
| 61 | + |
| 62 | + if !strings.Contains(editor, " ") { |
| 63 | + return []string{editor}, false |
| 64 | + } |
| 65 | + |
| 66 | + if !strings.ContainsAny(editor, "\"'\\") { |
| 67 | + return strings.Split(editor, " "), false |
| 68 | + } |
| 69 | + |
| 70 | + shell := defaultEnvShell() |
| 71 | + return append(shell, editor), true |
| 72 | +} |
| 73 | + |
| 74 | +func buildEditorCmd(filePath string) *exec.Cmd { |
| 75 | + args, shell := resolveEditor() |
| 76 | + |
| 77 | + if shell { |
| 78 | + // The editor string is the last element — append the file path to it |
| 79 | + safeFilePath := strings.ReplaceAll(filePath, "'", "'\\''") |
| 80 | + args[len(args)-1] = fmt.Sprintf("%s '%s'", args[len(args)-1], safeFilePath) |
| 81 | + } else { |
| 82 | + args = append(args, filePath) |
| 83 | + } |
| 84 | + |
| 85 | + //nolint:gosec // $EDITOR is a user-controlled local env var, same trust model as git/kubectl |
| 86 | + cmd := exec.Command(args[0], args[1:]...) |
| 87 | + cmd.Stdin = os.Stdin |
| 88 | + cmd.Stdout = os.Stdout |
| 89 | + cmd.Stderr = os.Stderr |
| 90 | + return cmd |
| 91 | +} |
| 92 | + |
| 93 | +func runEditor(content string) (string, error) { |
| 94 | + tmpFile, err := os.CreateTemp("", "docker-model-prompt-*.txt") |
| 95 | + if err != nil { |
| 96 | + return content, err |
| 97 | + } |
| 98 | + defer os.Remove(tmpFile.Name()) |
| 99 | + |
| 100 | + if _, err := tmpFile.WriteString(content); err != nil { |
| 101 | + tmpFile.Close() |
| 102 | + return content, err |
| 103 | + } |
| 104 | + tmpFile.Close() |
| 105 | + |
| 106 | + cmd := buildEditorCmd(tmpFile.Name()) |
| 107 | + if err := cmd.Run(); err != nil { |
| 108 | + return content, err |
| 109 | + } |
| 110 | + |
| 111 | + edited, err := os.ReadFile(tmpFile.Name()) |
| 112 | + if err != nil { |
| 113 | + return content, err |
| 114 | + } |
| 115 | + |
| 116 | + return strings.TrimRight(string(edited), "\r\n"), nil |
| 117 | +} |
0 commit comments