diff options
-rw-r--r-- | lib/pleroma/utils.ex | 15 | ||||
-rw-r--r-- | test/utils_test.exs | 13 |
2 files changed, 28 insertions, 0 deletions
diff --git a/lib/pleroma/utils.ex b/lib/pleroma/utils.ex index 6b8e3accf..21d1159be 100644 --- a/lib/pleroma/utils.ex +++ b/lib/pleroma/utils.ex @@ -9,4 +9,19 @@ defmodule Pleroma.Utils do |> Enum.map(&Path.join(dir, &1)) |> Kernel.ParallelCompiler.compile() end + + @doc """ + POSIX-compliant check if command is available in the system + + ## Examples + iex> command_available?("git") + true + iex> command_available?("wrongcmd") + false + + """ + @spec command_available?(String.t()) :: boolean() + def command_available?(command) do + match?({_output, 0}, System.cmd("sh", ["-c", "command -v #{command}"])) + end end diff --git a/test/utils_test.exs b/test/utils_test.exs new file mode 100644 index 000000000..6134c21b5 --- /dev/null +++ b/test/utils_test.exs @@ -0,0 +1,13 @@ +defmodule Pleroma.UtilsTest do + use ExUnit.Case + + describe "command_available?" do + test "available command" do + assert Pleroma.Utils.command_available?("iex") === true + end + + test "unavailable command" do + assert Pleroma.Utils.command_available?("nonexistingcmd") === false + end + end +end |