Packages
deli
0.1.27
0.2.0-rc.6
0.2.0-rc.5
0.2.0-rc.4
0.2.0-rc.3
0.2.0-rc.2
0.2.0-rc.1
0.1.28
0.1.27
0.1.26
0.1.25
0.1.23
0.1.22
0.1.21
0.1.20
0.1.19
0.1.18
0.1.17
0.1.15
0.1.14
0.1.13
0.1.12
0.1.11
0.1.10
0.1.9
0.1.8
retired
0.1.7
retired
0.1.6
retired
0.1.5
retired
0.1.4
retired
0.1.3
retired
0.1.2
retired
0.1.1
retired
0.1.0
retired
Easy deployment for Elixir apps
Current section
Files
Jump to
Current section
Files
lib/deli/host_filter.ex
defmodule Deli.HostFilter do
import Deli.Shell
alias Deli.Config
@moduledoc false
def hosts(env, args, silent? \\ false) do
hosts = env |> Config.hosts()
with %Regex{} = exp <- args |> host_filter do
with [_ | _] = filtered_hosts <- hosts |> Enum.filter(&(&1 =~ exp)) do
unless silent?, do: list_hosts(filtered_hosts)
{:ok, filtered_hosts}
else
[] ->
error!("""
Host filter #{inspect(exp)} excluded all hosts!
#{hosts_line(hosts)}
""")
end
else
nil ->
case hosts do
[] ->
error!("No hosts defined for target #{env}")
_ ->
unless silent?, do: list_hosts(hosts)
{:ok, hosts}
end
end
end
def host(env, args) do
{:ok, hosts} = env |> hosts(args, true)
length = hosts |> Enum.count()
case length do
0 ->
{:error, :no_host_found}
1 ->
host = hosts |> Enum.at(0)
{:ok, host}
_ ->
hosts |> select_host
end
end
defp select_host(hosts) do
hosts |> Enum.with_index() |> Enum.each(&print_host_option/1)
count = hosts |> Enum.count()
{number, ""} =
"Choose a number:"
|> Mix.shell().prompt()
|> String.trim()
|> Integer.parse()
if number < count do
host = hosts |> Enum.at(number)
{:ok, host}
else
select_host(hosts)
end
end
defp print_host_option({host, index}) do
IO.puts([
IO.ANSI.bright(),
"[#{index}] ",
IO.ANSI.reset(),
IO.ANSI.yellow(),
host,
IO.ANSI.reset()
])
end
defp host_filter(args) do
filter =
args
|> OptionParser.parse(aliases: [h: :host], switches: [host: :string])
|> elem(0)
|> Keyword.get(:host)
if filter, do: ~r/#{filter}/
end
defp list_hosts(hosts) do
IO.puts(hosts_line(hosts))
end
defp hosts_line(hosts) do
list = hosts |> Enum.map(&"## #{&1}") |> Enum.join("\n")
"# hosts\n#{list}"
end
end