| 1 |
module Command |
| 2 |
// from https://alexn.org/snippets/2020/12/06/execute-shell-command-in-fsharp.html |
| 3 |
|
| 4 |
open System.Diagnostics |
| 5 |
open System.Threading.Tasks |
| 6 |
|
| 7 |
type CommandResult = { |
| 8 |
ExitCode: int; |
| 9 |
StandardOutput: string; |
| 10 |
StandardError: string |
| 11 |
} |
| 12 |
|
| 13 |
let executeCommand executable args = |
| 14 |
async { |
| 15 |
let startInfo = ProcessStartInfo() |
| 16 |
startInfo.FileName <- executable |
| 17 |
for a in args do |
| 18 |
startInfo.ArgumentList.Add(a) |
| 19 |
startInfo.RedirectStandardOutput <- true |
| 20 |
startInfo.RedirectStandardError <- true |
| 21 |
startInfo.UseShellExecute <- false |
| 22 |
startInfo.CreateNoWindow <- true |
| 23 |
use p = new Process() |
| 24 |
p.StartInfo <- startInfo |
| 25 |
p.Start() |> ignore |
| 26 |
|
| 27 |
let outTask = Task.WhenAll([| |
| 28 |
p.StandardOutput.ReadToEndAsync(); |
| 29 |
p.StandardError.ReadToEndAsync() |
| 30 |
|]) |
| 31 |
|
| 32 |
do! p.WaitForExitAsync() |> Async.AwaitTask |
| 33 |
let! out = outTask |> Async.AwaitTask |
| 34 |
return { |
| 35 |
ExitCode = p.ExitCode; |
| 36 |
StandardOutput = out.[0]; |
| 37 |
StandardError = out.[1] |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
let executeShellCommand command = |
| 42 |
executeCommand "/usr/bin/env" [ "-S"; "bash"; "-c"; command ] |