-
-
Save fishcakez/5c0fa015fee1cbebec08 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@proto_version "1.0" | |
def process_options(opts) do | |
env_args() | |
|> (&process_in(opts, &1)).() | |
|> (&process_err(opts, &1)).() | |
|> (&process_dir(opts, &1)).() | |
end | |
defp process_in(opts, args) do | |
case Keyword.get(opts, :in, false) do | |
true -> ["-in" | args] | |
false -> args | |
end | |
end | |
defp process_err(opts, args) do | |
case Keyword.get(opts, :err, false) do | |
:out -> ["-err", "out" | args] | |
:err -> ["-err", "err" | args] | |
false -> args | |
end | |
end | |
defp process_dir(opts, args) do | |
case Keyword.get(opts, :dir, nil) do | |
dir when is_binary(dir) -> ["-dir", dir | args] | |
nil -> args | |
end | |
end | |
defp env_args() do | |
["-proto", @proto_version | log_args()] | |
end | |
defp log_args() do | |
case Application.fetch_env(:porcelain, :goon_driver_log) do | |
:error -> | |
[] | |
{:ok, val} -> | |
["-log", val] | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@proto_version "1.0" | |
def process_options(opts) do | |
Enum.reverse(opts) | |
|> Enum.reduce(%{in: [], err: [], dir: []}, &process_options/2) | |
|> Enum.reduce(env_args(), fn({_key, args}, acc) -> args ++ acc end) | |
end | |
defp process_options({key, value}, map) do | |
Map.put(map, key, process_option(key, value)) | |
end | |
defp process_option(:in, true), do: ["-in"] | |
defp process_option(:in, false), do: [] | |
defp process_option(:err, :out), do: ["-err", "out"] | |
defp process_option(:err, :err), do: ["-err", "err"] | |
defp process_option(:err, false), do: [] | |
defp process_option(:dir, path) when is_binary(path), do: ["-dir", path] | |
defp process_option(:dir, nil), do: [] | |
defp process_option(key, value) do | |
raise ArgumentError, "invalid option: #{inspect(key)}: #{inspect(value)}" | |
end | |
defp env_args() do | |
["-proto", @proto_version | log_args()] | |
end | |
defp log_args() do | |
case Application.fetch_env(:porcelain, :goon_driver_log) do | |
:error -> | |
[] | |
{:ok, val} -> | |
["-log", val] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment