From 080aefaefab4157580e5482fcce6bac1b5e5a353 Mon Sep 17 00:00:00 2001 From: akash-akya Date: Fri, 31 Jan 2025 10:13:52 +0530 Subject: [PATCH] Accept string and binary as input --- lib/ex_cmd.ex | 42 ++++++++++++++++++++++++++++++++++++++++-- lib/ex_cmd/stream.ex | 6 +++++- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/lib/ex_cmd.ex b/lib/ex_cmd.ex index 1f7799a..0c141c2 100644 --- a/lib/ex_cmd.ex +++ b/lib/ex_cmd.ex @@ -13,6 +13,37 @@ defmodule ExCmd do "Hello\n" ``` + String input + ``` + iex> ExCmd.stream!(~w(cat), input: "Hello World") + iex> |> Enum.into("") + "Hello World" + ``` + + List of strings + + ``` + iex> ExCmd.stream!(~w(cat), input: ["Hello", " ", "World"]) + iex> |> Enum.into("") + "Hello World" + ``` + + Binary data + + ``` + iex> ExCmd.stream!(~w(base64), input: <<1, 2, 3, 4, 5>>) + iex> |> Enum.into("") + "AQIDBAU=\n" + ``` + + IOData + + ``` + iex> ExCmd.stream!(~w(base64), input: [<<1, 2>>, [3], [<<4, 5>>]]) + iex> |> Enum.into("") + "AQIDBAU=\n" + ``` + Run a command with list of strings as input ``` @@ -174,6 +205,13 @@ defmodule ExCmd do ### Options * `input` - Input can be either an `Enumerable` or a function which accepts `Collectable`. + * String or Binary: + ``` + # List + ExCmd.stream!(~w(cat), input: "Hello World") |> Enum.into("") + # Stream + ExCmd.stream!(~w(cat), input: <<1, 2, 3, 4, 5>>) |> Enum.into("") + ``` * Enumerable: @@ -241,7 +279,7 @@ defmodule ExCmd do @type collectable_func() :: (Collectable.t() -> any()) @spec stream!(nonempty_list(String.t()), - input: Enum.t() | collectable_func(), + input: Enum.t() | collectable_func() | String.t() | binary(), exit_timeout: timeout(), cd: String.t(), env: [{String.t(), String.t()}], @@ -264,7 +302,7 @@ defmodule ExCmd do examples. """ @spec stream(nonempty_list(String.t()), - input: Enum.t() | collectable_func(), + input: Enum.t() | collectable_func() | String.t() | binary(), exit_timeout: timeout(), cd: String.t(), env: [{String.t(), String.t()}], diff --git a/lib/ex_cmd/stream.ex b/lib/ex_cmd/stream.ex index de73893..66ec9d6 100644 --- a/lib/ex_cmd/stream.ex +++ b/lib/ex_cmd/stream.ex @@ -266,8 +266,12 @@ defmodule ExCmd.Stream do is_function(term, 1) -> {:ok, {:collectable, term}} + is_binary(term) -> + {:ok, {:enumerable, [term]}} + true -> - {:error, "`:input` must be either Enumerable or a function which accepts collectable"} + {:error, + ":input must be a string, an Enumerable, or a function that accepts a Collectable."} end end