This commit is contained in:
commit
4aa6c48530
52 changed files with 3781 additions and 0 deletions
95
src/Process/Buffered/BufferedCommandExtensions.cs
Normal file
95
src/Process/Buffered/BufferedCommandExtensions.cs
Normal file
|
@ -0,0 +1,95 @@
|
|||
using System.Text;
|
||||
|
||||
namespace Geekeey.Extensions.Process.Buffered;
|
||||
|
||||
/// <summary>
|
||||
/// Buffered execution model.
|
||||
/// </summary>
|
||||
public static class BufferedCommandExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Executes the command asynchronously with buffering.
|
||||
/// Data written to the standard output and standard error streams is decoded as text
|
||||
/// and returned as part of the result object.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This method can be awaited.
|
||||
/// </remarks>
|
||||
public static CommandTask<BufferedCommandResult> ExecuteBufferedAsync(this Command command,
|
||||
Encoding standardOutputEncoding, Encoding standardErrorEncoding, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var stdOutBuffer = new StringBuilder();
|
||||
var stdErrBuffer = new StringBuilder();
|
||||
|
||||
var stdOutPipe = PipeTarget.Merge(
|
||||
command.StandardOutputPipe,
|
||||
PipeTarget.ToStringBuilder(stdOutBuffer, standardOutputEncoding)
|
||||
);
|
||||
|
||||
var stdErrPipe = PipeTarget.Merge(
|
||||
command.StandardErrorPipe,
|
||||
PipeTarget.ToStringBuilder(stdErrBuffer, standardErrorEncoding)
|
||||
);
|
||||
|
||||
var commandWithPipes = command
|
||||
.WithStandardOutputPipe(stdOutPipe)
|
||||
.WithStandardErrorPipe(stdErrPipe);
|
||||
|
||||
return commandWithPipes
|
||||
.ExecuteAsync(cancellationToken)
|
||||
.Bind(async task =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await task;
|
||||
|
||||
return new BufferedCommandResult(
|
||||
result.ExitCode,
|
||||
result.StartTime,
|
||||
result.ExitTime,
|
||||
stdOutBuffer.ToString(),
|
||||
stdErrBuffer.ToString()
|
||||
);
|
||||
}
|
||||
catch (CommandExecutionException exception)
|
||||
{
|
||||
var message = $"""
|
||||
Command execution failed, see the inner exception for details.
|
||||
|
||||
Standard error:
|
||||
{stdErrBuffer.ToString().Trim()}
|
||||
""";
|
||||
throw new CommandExecutionException(exception.Command, exception.ExitCode, message, exception);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes the command asynchronously with buffering.
|
||||
/// Data written to the standard output and standard error streams is decoded as text
|
||||
/// and returned as part of the result object.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This method can be awaited.
|
||||
/// </remarks>
|
||||
public static CommandTask<BufferedCommandResult> ExecuteBufferedAsync(this Command command,
|
||||
Encoding encoding, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return command.ExecuteBufferedAsync(encoding, encoding, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes the command asynchronously with buffering.
|
||||
/// Data written to the standard output and standard error streams is decoded as text
|
||||
/// and returned as part of the result object.
|
||||
/// Uses <see cref="Console.OutputEncoding" /> for decoding.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This method can be awaited.
|
||||
/// </remarks>
|
||||
public static CommandTask<BufferedCommandResult> ExecuteBufferedAsync(this Command command,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
return command.ExecuteBufferedAsync(Console.OutputEncoding, cancellationToken);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue