Skip to content

Commit

Permalink
Implement CommandExists with shell commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Perry committed Aug 25, 2023
1 parent 3f8bcae commit fb8b3f6
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions CommandExec/CommandUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ public static class CommandUtils
{
public static bool CommandExists(string command)
{
if (File.Exists(command))
return true;

string values = Environment.GetEnvironmentVariable("PATH") ?? throw new Exception("PATH variable not set");
foreach (var path in values.Split(Path.PathSeparator))
Command cmd;
if (Command.isUnix)
{
cmd = Command.Shell($"command -v {command} &> /dev/null && echo true").RedirectStdOut();
}
else
{
string fullPath = Path.Combine(path, command);
if (File.Exists(fullPath))
return true;
cmd = Command.Shell($"if (Get-Command -Name {command} -ErrorAction SilentlyContinue) {{echo true}}").RedirectStdOut();
}

return false;
cmd.Run();
return cmd.STDOut.ReadToEnd().ReplaceLineEndings("") == "true";
}
}
}

0 comments on commit fb8b3f6

Please sign in to comment.