Skip to content

Commit

Permalink
Merge pull request #41 from akash-akya/dev
Browse files Browse the repository at this point in the history
Accept string and binary as input
  • Loading branch information
akash-akya authored Jan 31, 2025
2 parents 35639a2 + 080aefa commit ae8d61e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
42 changes: 40 additions & 2 deletions lib/ex_cmd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()}],
Expand All @@ -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()}],
Expand Down
6 changes: 5 additions & 1 deletion lib/ex_cmd/stream.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit ae8d61e

Please sign in to comment.