Skip to content

Commit

Permalink
Allow passing an input stream to use as stdin.
Browse files Browse the repository at this point in the history
Certain cli programs will accept stdin as input and this enables that.
  • Loading branch information
adampoit committed Sep 21, 2024
1 parent ba6bdd3 commit 2cb8b47
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Faithlife.Build/AppRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ private static int DoRunApp(string path, AppRunnerSettings settings)

var handleOutputLine = settings.HandleOutputLine;
var handleErrorLine = settings.HandleErrorLine;
var inputStream = settings.InputStream;

var startInfo = new ProcessStartInfo
{
Expand All @@ -135,6 +136,7 @@ private static int DoRunApp(string path, AppRunnerSettings settings)
UseShellExecute = false,
RedirectStandardOutput = handleOutputLine is not null,
RedirectStandardError = handleErrorLine is not null,
RedirectStandardInput = inputStream is not null,
CreateNoWindow = false,
};

Expand Down Expand Up @@ -180,6 +182,14 @@ private static int DoRunApp(string path, AppRunnerSettings settings)

process.Start();

if (inputStream is not null)
{
using var reader = new StreamReader(inputStream);

process.StandardInput.Write(reader.ReadToEnd());
process.StandardInput.Close();
}

if (handleOutputLine is not null)
process.BeginOutputReadLine();

Expand Down
5 changes: 5 additions & 0 deletions src/Faithlife.Build/AppRunnerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public sealed class AppRunnerSettings
/// </summary>
public Action<string>? HandleErrorLine { get; set; }

/// <summary>
/// If set, redirects standard in and uses this stream as input.
/// </summary>
public Stream? InputStream { get; set; }

/// <summary>
/// Clones the settings.
/// </summary>
Expand Down

0 comments on commit 2cb8b47

Please sign in to comment.