public async Task RunProcessAndCaptureOuputAsync()

in src/Aspire.Hosting.AWS/Utils/Internal/ProcessCommandService.cs [54:111]


    public async Task<IProcessCommandService.RunProcessAndCaptureStdOutResult> RunProcessAndCaptureOuputAsync(ILogger logger, string path, string arguments, CancellationToken cancellationToken)
    {
        using var process = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                WorkingDirectory = Directory.GetCurrentDirectory(),
                FileName = path,
                Arguments = arguments,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                WindowStyle = ProcessWindowStyle.Hidden
            }
        };

        var output = new StringBuilder();

        process.OutputDataReceived += (sender, e) =>
        {
            if (e.Data != null)
            {
                output.Append(e.Data);
            }
        };

        process.ErrorDataReceived += (sender, e) =>
        {
            if (e.Data != null)
            {
                output.Append(e.Data);
            }
        };

        try
        {
            process.Start();
            process.BeginOutputReadLine();
            process.BeginErrorReadLine();
        }
        catch (Exception ex)
        {
            // If this fails then it most likey means the executable being invoked does not exist.
            logger.LogDebug(ex, "Failed to start process {process}.", path);
            return new IProcessCommandService.RunProcessAndCaptureStdOutResult(-404, string.Empty);
        }

        await process.WaitForExitAsync(cancellationToken);

        if (process.ExitCode != 0)
        {
            logger.LogDebug("Process {process} exited with code {exitCode}.", path, process.ExitCode);
            return new IProcessCommandService.RunProcessAndCaptureStdOutResult(process.ExitCode, output.ToString());
        }

        return new IProcessCommandService.RunProcessAndCaptureStdOutResult(process.ExitCode, output.ToString());

    }