using System.Diagnostics; using CliWrap; namespace InnovEnergy.Lib.Utils; public static class CliCommandUtils { public static Command OnHost(this Command command, SshHost? sshHost) { return sshHost is not null ? sshHost.Command.AppendArguments(command.TargetFilePath, command.Arguments) : command; } public static Command AppendArgument(this Command command, String arg) { return command.WithArguments($"{command.Arguments} {arg}"); } public static Command AppendArguments(this Command command, params String[] args) { return args .Aggregate(command.Arguments, (a, b) => a + " " + b) .Apply(command.WithArguments); } public static Command PrependArgument(this Command command, String arg) { return command.WithArguments(arg + " " + command.Arguments); } public static async Task ExecuteInteractive(this Command command) { var startInfo = new ProcessStartInfo { RedirectStandardOutput = false, RedirectStandardError = false, RedirectStandardInput = false, UseShellExecute = false, CreateNoWindow = true, Arguments = command.Arguments, FileName = command.TargetFilePath, }; foreach (var kv in command.EnvironmentVariables) startInfo.EnvironmentVariables[kv.Key] = kv.Value; using var process = new Process { StartInfo = startInfo, EnableRaisingEvents = true }; process.Start(); await process.WaitForExitAsync(); return process.ExitCode; } }