Skip to content

Commit

Permalink
VIP - improve cmd output
Browse files Browse the repository at this point in the history
  • Loading branch information
shahryarjb committed Jun 25, 2024
1 parent 6e7a55f commit f34f7ab
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
20 changes: 20 additions & 0 deletions lib/installer/collect.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule MishkaInstaller.Installer.Collect do
@moduledoc false
# Based on
# https://elixir-lang.slack.com/archives/C03EPRA3B/p1719319847338759?thread_ts=1719318192.858049&cid=C03EPRA3B
defstruct [:callback, acc: nil]

defimpl Collectable do
alias MishkaInstaller.Installer.Collect

def into(%Collect{acc: acc, callback: callback}) do
collector_fun = fn
acc, {:cont, elem} -> callback.(elem, acc)
acc, :done -> acc
_acc, :halt -> :ok
end

{acc, collector_fun}
end
end
end
17 changes: 15 additions & 2 deletions lib/installer/library_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule MishkaInstaller.Installer.LibraryHandler do
@moduledoc """
"""
alias MishkaInstaller.Installer.Installer
alias MishkaInstaller.Installer.{Installer, Collect}

@type posix :: :file.posix()

Expand Down Expand Up @@ -146,11 +146,24 @@ defmodule MishkaInstaller.Installer.LibraryHandler do

{stream, status} =
System.cmd(operation, [command],
into: IO.stream(),
into: %Collect{
callback: fn line, _acc ->
MishkaInstaller.broadcast("library_handler", :cmd_messaging, %{
operation: command,
message: "#{inspect(line)}",
status: :looping
})

IO.puts("[stdout] #{line}")
end
},
stderr_to_stdout: true,
env: [{"MIX_ENV", "#{info.env}"}, {"PROJECT_PATH", "#{info.path}"}]
)

return_exist = %{operation: command, output: stream, status: status}
MishkaInstaller.broadcast("library_handler", :cmd_stopped, return_exist)

if status == 0 do
:ok
else
Expand Down

0 comments on commit f34f7ab

Please sign in to comment.