in src/AWS.Deploy.CLI/Utilities/CommandLineWrapper.cs [40:136]
public async Task Run(
string command,
string workingDirectory = "",
bool streamOutputToInteractiveService = true,
Action<TryRunResult>? onComplete = null,
bool redirectIO = true,
string? stdin = null,
IDictionary<string, string>? environmentVariables = null,
bool needAwsCredentials = false,
CancellationToken cancellationToken = default)
{
StringBuilder strOutput = new StringBuilder();
StringBuilder strError = new StringBuilder();
var processStartInfo = new ProcessStartInfo
{
FileName = GetSystemShell(),
Arguments =
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? $"/c {command}"
: $"-c \"{command}\"",
RedirectStandardInput = redirectIO || stdin != null,
RedirectStandardOutput = redirectIO,
RedirectStandardError = redirectIO,
UseShellExecute = false,
CreateNoWindow = redirectIO,
WorkingDirectory = workingDirectory
};
// If the command output is not being redirected check to see if
// the output should go to a separate console window. This is important when run from
// an IDE which won't have a console window by default.
if (!streamOutputToInteractiveService && !redirectIO)
{
processStartInfo.UseShellExecute = _useSeparateWindow;
}
UpdateEnvironmentVariables(processStartInfo, environmentVariables);
if (needAwsCredentials)
_processStartInfoAction?.Invoke(processStartInfo);
var process = Process.Start(processStartInfo);
if (null == process)
throw new Exception("Process.Start failed to return a non-null process");
if (redirectIO)
{
process.OutputDataReceived += (sender, e) =>
{
if(streamOutputToInteractiveService)
{
_interactiveService.LogInfoMessage(e.Data);
}
strOutput.AppendLine(e.Data);
};
process.ErrorDataReceived += (sender, e) =>
{
if(streamOutputToInteractiveService)
{
_interactiveService.LogInfoMessage(e.Data);
}
strError.AppendLine(e.Data);
};
process.BeginOutputReadLine();
process.BeginErrorReadLine();
}
if(stdin != null)
{
process.StandardInput.Write(stdin);
process.StandardInput.Close();
}
await process.WaitForExitAsync(cancellationToken);
if (onComplete != null)
{
var result = new TryRunResult
{
ExitCode = process.ExitCode
};
if (redirectIO)
{
result.StandardError = strError.ToString();
result.StandardOut = strOutput.ToString();
}
onComplete(result);
}
}