如何将一个命令的输出通过管道传输到 Elixir 的混合任务中?

How to unix pipe the output of one command into Elixir's Mix Task?

我想 "pipe" cat 命令的输出到 Elixir Mix 任务中,并将其作为二进制保存在变量中。

我已经尝试使用 IO.gets/1,但它只读取输出的第一行。

cat textfile.txt | mix print
defmodule Mix.Tasks.Print do
  use Mix.Task

  def run(_argv) do
    Task.async(fn -> IO.gets("") end)
    |> Task.await(t)
    |> IO.puts() # prints the first line
  end
end

我想在 Elixir 的二进制变量中获取整个文件的内容并将其打印到控制台,但我只得到第一行。我希望 Elixir 有一些内置的解决方案,以 EOF 结束。

有一个 IO.read/2 函数是我要找的。

defmodule Mix.Tasks.Print do
  use Mix.Task

  def run(_argv) do
    IO.read(:all)
    |> IO.puts() # prints all lines
  end
end