Created
July 2, 2018 02:08
-
-
Save sudix/12a8878391f0acbbb86471d3e161d3e8 to your computer and use it in GitHub Desktop.
ElixirでバッチっぽいことをするEndpointサンプル(引数受け取り)
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
defmodule MyApp do | |
def start(_type, _args) do | |
get_args() | |
|> run() | |
|> stop() | |
end | |
# 引数取得 | |
# 実行時の引数を受け取る場合は `:init.get_plain_arguments/0` を使う。 | |
# ``` | |
# $ _build/dev/rel/my_app/bin/my_app foreground --flow hoge | |
# ``` | |
# で実行すると、foregroundの後で指定した引数(`--flow hoge`)が取れる。 | |
defp get_args() do | |
{parsed, _argv} = | |
:init.get_plain_arguments() | |
|> parse_args | |
parsed | |
end | |
defp parse_args([_ | [args]]) do | |
args | |
|> to_string | |
|> OptionParser.split() | |
|> OptionParser.parse!(switches: [flow: :string]) | |
end | |
def run([flow: "hoge"] = _args) do | |
IO.puts("hoge!!!!!") | |
:ok | |
end | |
def run([flow: "fuga"] = _args) do | |
IO.puts("fuga!!!!!") | |
:ok | |
end | |
def run(_args) do | |
IO.puts("no args") | |
:error | |
end | |
defp stop(:ok), do: System.stop(0) | |
defp stop(_), do: System.stop(1) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
distilleryでビルドしたElixirアプリをバッチ的に実行したい場合のEndpointサンプル。