(command, cwd, executable string)
| 200 | |
| 201 | func (p *PromptProcessor) runInlineShell(command, cwd, executable string) (string, error) { |
| 202 | runCWD := cwd |
| 203 | if runCWD == "" { |
| 204 | runCWD = p.Config.CWD |
| 205 | } |
| 206 | timeout := time.Duration(p.Config.ShellTimeoutSeconds * float64(time.Second)) |
| 207 | ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| 208 | defer cancel() |
| 209 | argv := shellArgv(command, executable) |
| 210 | cmd := exec.CommandContext(ctx, argv[0], argv[1:]...) |
| 211 | cmd.Dir = runCWD |
| 212 | var stdout, stderr strings.Builder |
| 213 | cmd.Stdout = &stdout |
| 214 | cmd.Stderr = &stderr |
| 215 | err := cmd.Run() |
| 216 | if ctx.Err() == context.DeadlineExceeded { |
| 217 | return "", fmt.Errorf("Inline shell command timed out after %ss: %s", pythonFloatString(p.Config.ShellTimeoutSeconds), command) |
| 218 | } |
| 219 | if err != nil { |
| 220 | if cmd.ProcessState == nil { |
| 221 | return "", err |
| 222 | } |
| 223 | return "", fmt.Errorf("Inline shell command failed (%d): %s\n%s", cmd.ProcessState.ExitCode(), command, strings.TrimSpace(strings.ToValidUTF8(stderr.String(), "\uFFFD"))) |
| 224 | } |
| 225 | out := strings.TrimSpace(strings.ToValidUTF8(stdout.String(), "\uFFFD")) |
| 226 | if len([]byte(out)) > p.Config.ShellMaxOutputBytes { |
| 227 | return "", fmt.Errorf("Inline shell command output exceeded %d bytes: %s", p.Config.ShellMaxOutputBytes, command) |
| 228 | } |
| 229 | return out, nil |
| 230 | } |
| 231 | |
| 232 | func shellArgv(command, executable string) []string { |
| 233 | return bashpkg.ShellArgv(command, executable) |
| 234 | } |
no test coverage detected