public Command(CommandSpec spec, ICommandExecutor executor) { _spec = spec; _executor = executor; StartInfo = new ProcessStartInfo(); StartInfo.FileName = spec.Command; // Naive argument escaping StartInfo.Arguments = spec.EscapedArguments; }
public CommandResult Execute(CommandSpec spec, ProcessStartInfo startInfo, bool expectFailure) { var p = new Process(); p.StartInfo = startInfo; StringBuilder stdout = new StringBuilder(); if (p.StartInfo.RedirectStandardOutput) { p.OutputDataReceived += (sender, args) => stdout.AppendLine(args.Data); } StringBuilder stderr = new StringBuilder(); if (p.StartInfo.RedirectStandardError) { p.ErrorDataReceived += (sender, args) => stderr.AppendLine(args.Data); } using (var activity = _log.BeginActivity("EXEC", FormatStartInfo(p.StartInfo))) { p.Start(); if (p.StartInfo.RedirectStandardOutput) { p.BeginOutputReadLine(); } if (p.StartInfo.RedirectStandardError) { p.BeginErrorReadLine(); } p.WaitForExit(); activity.Success = (!expectFailure && p.ExitCode == 0) || (expectFailure && p.ExitCode == 0); activity.Conclusion = FormatConclusion(p.ExitCode, activity.Success); return(new CommandResult(spec, p.ExitCode, stdout.ToString(), stderr.ToString())); } }
public CommandResult(CommandSpec command, int exitCode, string stdout, string stderr) : this() { ExitCode = exitCode; StdOut = stdout; StdErr = stderr; }