Install commands, improvements

This commit is contained in:
2022-11-06 18:36:04 +01:00
parent 8fd6b526f8
commit 30c3266e25
15 changed files with 278 additions and 87 deletions

View File

@@ -0,0 +1,52 @@
using System.Diagnostics;
using System.Runtime.InteropServices;
using Alma.Logging;
namespace Alma.Services;
public class ShellService : IShellService
{
private readonly ILogger<ShellService> _logger;
public ShellService(ILogger<ShellService> logger)
{
_logger = logger;
}
public async Task RunCommandAsync(string command)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
var processStartInfo = new ProcessStartInfo()
{
FileName = "sh",
ArgumentList = {"-c", command},
RedirectStandardOutput = true,
RedirectStandardInput = true,
CreateNoWindow = true,
UseShellExecute = false
};
var process = Process.Start(processStartInfo);
if (process is null) return;
var reader = process.StandardOutput;
while (!reader.EndOfStream)
{
var content = await reader.ReadLineAsync();
if (content is not null)
{
_logger.LogInformation(content);
}
}
await process.WaitForExitAsync();
return;
}
_logger.LogError("Platform not supported");
throw new NotSupportedException();
}
}