From 9ff299487df9dacc6d77edb9e5117a7fbbb73555 Mon Sep 17 00:00:00 2001 From: Perry Date: Thu, 7 Sep 2023 00:38:41 -0300 Subject: [PATCH 1/2] Use exit code instead of command output to check if command exists --- CommandExec/CommandUtils.cs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/CommandExec/CommandUtils.cs b/CommandExec/CommandUtils.cs index 2ae25d9..6271c52 100644 --- a/CommandExec/CommandUtils.cs +++ b/CommandExec/CommandUtils.cs @@ -1,6 +1,3 @@ -using System; -using System.IO; - namespace CommandExec { public static class CommandUtils @@ -10,15 +7,21 @@ public static bool CommandExists(string command) Command cmd; if (Command.isUnix) { - cmd = Command.Shell($"command -v {command} &> /dev/null && echo true").RedirectStdOut(); + cmd = Command.Shell($"command -v {command} &> /dev/null").RedirectStdOut() + .RedirectStdOut() + .RedirectStdErr() + .RedirectStdIn(); } else { - cmd = Command.Shell($"if (Get-Command -Name {command} -ErrorAction SilentlyContinue) {{echo true}}").RedirectStdOut(); + cmd = Command.Shell($"Get-Command -Name {command} -ErrorAction SilentlyContinue | $null") + .RedirectStdOut() + .RedirectStdErr() + .RedirectStdIn(); } cmd.Run(); - return cmd.STDOut.ReadToEnd().ReplaceLineEndings("") == "true"; + return cmd.process.ExitCode == 0; } } } From 03496a9486a097c5092870bea49444f07909e1fc Mon Sep 17 00:00:00 2001 From: Perry Date: Thu, 7 Sep 2023 00:47:01 -0300 Subject: [PATCH 2/2] Fix piping to null on powershell --- CommandExec/CommandUtils.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CommandExec/CommandUtils.cs b/CommandExec/CommandUtils.cs index 6271c52..4b1b839 100644 --- a/CommandExec/CommandUtils.cs +++ b/CommandExec/CommandUtils.cs @@ -14,7 +14,7 @@ public static bool CommandExists(string command) } else { - cmd = Command.Shell($"Get-Command -Name {command} -ErrorAction SilentlyContinue | $null") + cmd = Command.Shell($"Get-Command -Name {command} -ErrorAction SilentlyContinue > $null") .RedirectStdOut() .RedirectStdErr() .RedirectStdIn();