This commit is contained in:
2020-05-25 22:22:34 +00:00
parent a27066bbfd
commit a10a3e3069
4 changed files with 45 additions and 29 deletions

View File

@@ -10,22 +10,22 @@ namespace AyaNova.Util
{
public static string Run(string cmd, string arguments, ILogger log = null)
public static string Run(string cmd, string arguments, ILogger log = null, int waitForExitTimeOut = int.MaxValue)
{
try
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return RunWindows(cmd, arguments);
return RunWindows(cmd, arguments, waitForExitTimeOut);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return RunOSX(cmd, arguments);
return RunOSX(cmd, arguments, waitForExitTimeOut);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return RunLinuxBash(cmd, arguments);
return RunLinuxBash(cmd, arguments, waitForExitTimeOut);
}
}
catch (Exception ex)
@@ -46,35 +46,38 @@ namespace AyaNova.Util
private static string RunWindows(string cmd, string arguments)
private static string RunWindows(string cmd, string arguments, int waitForExitTimeOut = int.MaxValue)
{
//RunProgram.Run("cmd.exe",FullRunCommand, log);
// var FullRunCommand=$"/C {BackupUtilityCommand} {Arguments}";
//for Windows need to pass to cmd.exe because often have command line piping etc
var args=$"/C {cmd} {arguments}";
//RunProgram.Run("cmd.exe",FullRunCommand, log);
// var FullRunCommand=$"/C {BackupUtilityCommand} {Arguments}";
//for Windows need to pass to cmd.exe because often have command line piping etc
var args = $"/C {cmd} {arguments}";
using (var process = new Process())
{
process.StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = args,
Arguments = args,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
};
process.Start();
string result = $"{process.StandardOutput.ReadToEnd()}{process.StandardError.ReadToEnd()} ";
process.WaitForExit();
if (!process.WaitForExit(waitForExitTimeOut))
{
result += $"\nERROR: TIMED OUT {waitForExitTimeOut}ms BEFORE COMPLETION";
}
return result;
}
}
private static string RunLinuxBash(string cmd, string arguments)
private static string RunLinuxBash(string cmd, string arguments, int waitForExitTimeOut = int.MaxValue)
{
var escapedArgs = $"{cmd} {arguments}".Replace("\"", "\\\"");
@@ -91,13 +94,16 @@ namespace AyaNova.Util
process.Start();
string result = process.StandardOutput.ReadToEnd();
process.WaitForExit();
if (!process.WaitForExit(waitForExitTimeOut))
{
result += $"\nERROR: TIMED OUT {waitForExitTimeOut}ms BEFORE COMPLETION";
}
return result;
}
}
private static string RunOSX(string cmd, string arguments)
private static string RunOSX(string cmd, string arguments, int waitForExitTimeOut = int.MaxValue)
{
using (var process = new Process())
{
@@ -112,7 +118,10 @@ namespace AyaNova.Util
process.Start();
string result = process.StandardOutput.ReadToEnd();
process.WaitForExit();
if (!process.WaitForExit(waitForExitTimeOut))
{
result += $"\nERROR: TIMED OUT {waitForExitTimeOut}ms BEFORE COMPLETION";
}
return result;
}
}