From 375eb3dea54c1bff09bfc4ac7a3b4f931281fc84 Mon Sep 17 00:00:00 2001 From: akash-akya Date: Thu, 9 Jan 2025 11:48:31 +0530 Subject: [PATCH] Correct default stderr behaviour --- lib/ex_cmd.ex | 21 +++++++++++++++++---- lib/ex_cmd/process.ex | 10 +++++----- lib/ex_cmd/process/exec.ex | 2 +- lib/ex_cmd/stream.ex | 2 +- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/lib/ex_cmd.ex b/lib/ex_cmd.ex index e4bf32a..1f7799a 100644 --- a/lib/ex_cmd.ex +++ b/lib/ex_cmd.ex @@ -119,6 +119,14 @@ defmodule ExCmd do "X 250 X\n" ``` + With stderr set to :disable (default) + + ``` + iex> ExCmd.stream!(["sh", "-c", "echo foo; echo bar >&2"]) + ...> |> Enum.to_list() + ["foo\n"] + ``` + With stderr set to :redirect_to_stdout ``` @@ -127,14 +135,15 @@ defmodule ExCmd do "foo\nbar\n" ``` - With stderr set to :disable + With stderr set to :console ``` - iex> ExCmd.stream!(["sh", "-c", "echo foo; echo bar >&2"], stderr: :disable) - ...> |> Enum.to_list() - ["foo\n"] + ExCmd.stream!(["sh", "-c", "echo foo; echo bar >&2"], stderr: :console) + |> Enum.to_list() + # STDERR output will be written to the console ``` + For more details about stream API, see `ExCmd.stream!/2` and `ExCmd.stream/2`. For more details about inner working, please check `ExCmd.Process` @@ -234,6 +243,8 @@ defmodule ExCmd do @spec stream!(nonempty_list(String.t()), input: Enum.t() | collectable_func(), exit_timeout: timeout(), + cd: String.t(), + env: [{String.t(), String.t()}], stderr: :console | :redirect_to_stdout | :disable, ignore_epipe: boolean(), max_chunk_size: pos_integer() @@ -255,6 +266,8 @@ defmodule ExCmd do @spec stream(nonempty_list(String.t()), input: Enum.t() | collectable_func(), exit_timeout: timeout(), + cd: String.t(), + env: [{String.t(), String.t()}], stderr: :console | :redirect_to_stdout | :disable, ignore_epipe: boolean(), max_chunk_size: pos_integer() diff --git a/lib/ex_cmd/process.ex b/lib/ex_cmd/process.ex index 88bbdcf..2b61c6f 100644 --- a/lib/ex_cmd/process.ex +++ b/lib/ex_cmd/process.ex @@ -122,9 +122,9 @@ defmodule ExCmd.Process do You can change the behavior by setting `:stderr`: - 1. `:console` - stderr output is redirected to console (Default) + 1. `:console` - stderr output is redirected to console 2. `:redirect_to_stdout` - stderr output is redirected to stdout - 3. `:disable` - stderr output is redirected `/dev/null` suppressing all output. See below for more details. + 3. `:disable` - stderr output is redirected `/dev/null` suppressing all output. See below for more details. (Default) ### Using `redirect_to_stdout` @@ -347,9 +347,9 @@ defmodule ExCmd.Process do These can be accessed in the external program * `stderr` - different ways to handle stderr stream. - 1. `:console` - stderr output is redirected to console (Default) + 1. `:console` - stderr output is redirected to console 2. `:redirect_to_stdout` - stderr output is redirected to stdout - 3. `:disable` - stderr output is redirected `/dev/null` suppressing all output + 3. `:disable` - stderr output is redirected `/dev/null` suppressing all output (Default) See [`:stderr`](#module-stderr) for more details and issues associated with them @@ -361,7 +361,7 @@ defmodule ExCmd.Process do @spec start_link(nonempty_list(String.t()), cd: String.t(), env: [{String.t(), String.t()}], - stderr: :console | :disable | :stream + stderr: :console | :redirect_to_stdout | :disable ) :: {:ok, t} | {:error, any()} def start_link([cmd | args], opts \\ []) do opts = Keyword.merge(@default_opts, opts) diff --git a/lib/ex_cmd/process/exec.ex b/lib/ex_cmd/process/exec.ex index 3fb0ee2..8c2c00e 100644 --- a/lib/ex_cmd/process/exec.ex +++ b/lib/ex_cmd/process/exec.ex @@ -98,7 +98,7 @@ defmodule ExCmd.Process.Exec do defp normalize_stderr(stderr) do case stderr do nil -> - {:ok, :console} + {:ok, :disable} stderr when stderr in [:console, :disable, :redirect_to_stdout] -> {:ok, stderr} diff --git a/lib/ex_cmd/stream.ex b/lib/ex_cmd/stream.ex index 1b20c39..de73893 100644 --- a/lib/ex_cmd/stream.ex +++ b/lib/ex_cmd/stream.ex @@ -300,7 +300,7 @@ defmodule ExCmd.Stream do defp normalize_stderr(stderr) do case stderr do nil -> - {:ok, :console} + {:ok, :disable} stderr when stderr in [:console, :redirect_to_stdout, :disable] -> {:ok, stderr}