diff options
| author | Melodi <62381002+Melodi17@users.noreply.github.com> | 2026-10-27 23:04:59 +1000 |
|---|---|---|
| committer | Melodi <62381002+Melodi17@users.noreply.github.com> | 2026-10-27 23:04:59 +1000 |
| commit | ecd5f5d13936dd097b12766cb95a568ad5b073a9 (patch) | |
| tree | f360b08e656a98faca4f2b9f10dc44602e6ca289 /src/marlin.agents.shell/CommandRunner.cs | |
| parent | 88ee626cb3686dc176cd7911b073c4e65fa54d34 (diff) | |
feat: lots of stuff
Diffstat (limited to 'src/marlin.agents.shell/CommandRunner.cs')
| -rw-r--r-- | src/marlin.agents.shell/CommandRunner.cs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/marlin.agents.shell/CommandRunner.cs b/src/marlin.agents.shell/CommandRunner.cs new file mode 100644 index 0000000..d4fcbb3 --- /dev/null +++ b/src/marlin.agents.shell/CommandRunner.cs @@ -0,0 +1,38 @@ +using System.Diagnostics; + +public static class CommandRunner +{ + public static async Task<int> RunAsync(string command, Action<string> onStdout, Action<string> onStderr) + { + var psi = new ProcessStartInfo + { + FileName = GetShell(), + RedirectStandardInput = true, + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true + }; + + var process = new Process { StartInfo = psi }; + process.OutputDataReceived += (_, e) => { if (e.Data != null) onStdout(e.Data); }; + process.ErrorDataReceived += (_, e) => { if (e.Data != null) onStderr(e.Data); }; + + process.Start(); + process.BeginOutputReadLine(); + process.BeginErrorReadLine(); + + // Write the command to stdin and close it + await process.StandardInput.WriteLineAsync(command); + await process.StandardInput.WriteLineAsync("exit $?"); // Exit with the command's exit code + process.StandardInput.Close(); + + await process.WaitForExitAsync(); + return process.ExitCode; + } + + private static string GetShell() + { + return OperatingSystem.IsWindows() ? "cmd.exe" : "/bin/bash"; + } +}
\ No newline at end of file |
