using InnovEnergy.Lib.SysTools.Utils; namespace InnovEnergy.Lib.SysTools.Process; public readonly struct ProcessResult { public ProcessResult(Int32 exitCode, IReadOnlyList stdOut, IReadOnlyList stdErr, String output) { ExitCode = exitCode; StdOut = stdOut; StdErr = stdErr; Output = output; } public Int32 ExitCode { get; } public String Output { get; } public IReadOnlyList StdOut { get; } public IReadOnlyList 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 action) { if (Failed) action(this); return this; } public ProcessResult OnSuccess(Action 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; } }