Skip to content

Commit 935fdff

Browse files
beatrizChagaswevtimoteogaraujodev
committed
Introduce CommandRunner and CommandRunnerbehaviour
Co-authored-by: wevtimoteo <[email protected]> Co-authored-by: garaujodev <[email protected]> Co-authored-by: wevtimoteo <[email protected]> Co-authored-by: garaujodev <[email protected]>
1 parent d6d5917 commit 935fdff

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

lib/auto_formatter/action.ex

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ defmodule AutoFormatter.Action do
66
autoformatter _format
77
"""
88

9-
alias AutoFormatter.HookManager
9+
alias AutoFormatter.{CommandRunner, HookManager}
1010

1111
def perform(:version) do
1212
AutoFormatter.version()
@@ -17,7 +17,7 @@ defmodule AutoFormatter.Action do
1717
end
1818

1919
def perform(:_format) do
20-
{output, 0} = System.cmd("git", ["diff", "--name-only"])
20+
{:ok, output} = command_runner().run("git", ["diff", "--name-only"])
2121

2222
files_list =
2323
output
@@ -26,7 +26,7 @@ defmodule AutoFormatter.Action do
2626
|> tl()
2727
|> Enum.reverse()
2828

29-
{_output, 0} = System.cmd("mix", ["format" | files_list])
29+
{:ok, _output} = command_runner().run("mix", ["format" | files_list])
3030

3131
"Formatted successfully"
3232
end
@@ -38,4 +38,8 @@ defmodule AutoFormatter.Action do
3838
defp hook_manager do
3939
Application.get_env(:auto_formatter, :hook_manager, HookManager)
4040
end
41+
42+
defp command_runner do
43+
Application.get_env(:auto_formatter, :command_runner, CommandRunner)
44+
end
4145
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
defmodule AutoFormatter.CommandRunnerBehaviour do
2+
@callback run(String.t(), nonempty_list(String.t())) :: {atom(), String.t()}
3+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
defmodule AutoFormatter.CommandRunner do
2+
alias AutoFormatter.CommandRunnerBehaviour
3+
4+
@behaviour CommandRunnerBehaviour
5+
6+
@impl CommandRunnerBehaviour
7+
def run(command, args \\ [""]) do
8+
case System.cmd(command, args) do
9+
{output, 0} -> {:ok, output}
10+
{output, _exit_status} -> {:error, output}
11+
end
12+
end
13+
end

0 commit comments

Comments
 (0)