Innovenergy_trunk/csharp/lib/SysTools/Process/ProcessResult.cs

62 lines
1.4 KiB
C#

using InnovEnergy.SysTools.Utils;
namespace InnovEnergy.SysTools.Process;
public readonly struct ProcessResult
{
public ProcessResult(Int32 exitCode,
IReadOnlyList<String> stdOut,
IReadOnlyList<String> stdErr,
String output)
{
ExitCode = exitCode;
StdOut = stdOut;
StdErr = stdErr;
Output = output;
}
public Int32 ExitCode { get; }
public String Output { get; }
public IReadOnlyList<String> StdOut { get; }
public IReadOnlyList<String> StdErr { get; }
public ProcessResult ThrowOnError()
{
if (ExitCode != 0)
{
if (StdErr.Count ==0)
throw new Exception(nameof(AsyncProcess) + " exited with exit code " + ExitCode);
throw new Exception(StdErr.Aggregate((a, b) => a.NewLine() + b));
}
return this;
}
public ProcessResult OnError(Action<ProcessResult> action)
{
if (Failed) action(this);
return this;
}
public ProcessResult OnSuccess(Action<ProcessResult> action)
{
if (Succeeded) action(this);
return this;
}
public ProcessResult Dump()
{
Output.Write();
return this;
}
public Boolean Succeeded => ExitCode == 0;
public Boolean Failed => ExitCode != 0;
public override String ToString()
{
return Output.NewLine() + "ExitCode: " + ExitCode;
}
}