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

62 lines
1.4 KiB
C#
Raw Normal View History

using InnovEnergy.Lib.SysTools.Utils;
2023-02-16 12:57:06 +00:00
namespace InnovEnergy.Lib.SysTools.Process;
2023-02-16 12:57:06 +00:00
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;
}
}