feat(agent): Shell.run wrapper for testable external commands

This commit is contained in:
Carsten 2026-04-21 22:31:24 +02:00
parent 30b507ba6b
commit 6fca450d7e
2 changed files with 43 additions and 0 deletions

View file

@ -0,0 +1,24 @@
defmodule ProxmoxAgent.Shell do
@moduledoc """
Thin wrapper over System.cmd for testability. Collectors accept an optional
:runner function of this shape so tests can inject fixture-backed fakes.
"""
@type result :: {:ok, String.t()} | {:error, term()}
@spec run(String.t(), [String.t()]) :: result
def run(command, args) do
try do
case System.cmd(command, args, stderr_to_stdout: true) do
{output, 0} -> {:ok, output}
{output, code} -> {:error, {:nonzero_exit, code, output}}
end
rescue
e in ErlangError ->
case e.original do
:enoent -> {:error, {:enoent, command}}
other -> {:error, {:system_error, other}}
end
end
end
end