Skip to content

Instantly share code, notes, and snippets.

@fishcakez
Forked from josevalim/exercise.exs
Last active August 29, 2015 14:02
Show Gist options
  • Save fishcakez/5c0fa015fee1cbebec08 to your computer and use it in GitHub Desktop.
Save fishcakez/5c0fa015fee1cbebec08 to your computer and use it in GitHub Desktop.
@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
@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