initialize_command.ex (1501B)
1 defmodule Credo.Execution.Task.InitializeCommand do 2 @moduledoc false 3 4 alias Credo.Execution 5 6 def call(%Execution{} = exec, _opts) do 7 command_name = Execution.get_command_name(exec) 8 command_mod = Execution.get_command(exec, command_name) 9 10 init_command(exec, command_mod) 11 end 12 13 defp init_command(exec, command_mod) do 14 # exec = 15 # exec 16 # |> command_mod.init() 17 # |> Execution.ensure_execution_struct("#{command_mod}.init/1") 18 19 exec = 20 command_mod 21 |> cli_options_switches() 22 |> Enum.reduce(exec, fn {switch_name, switch_type}, exec -> 23 Execution.put_cli_switch(exec, command_mod, switch_name, switch_type) 24 end) 25 26 command_mod 27 |> cli_options_aliases() 28 |> Enum.reduce(exec, fn {switch_alias, switch_name}, exec -> 29 Execution.put_cli_switch_alias(exec, command_mod, switch_name, switch_alias) 30 end) 31 end 32 33 defp cli_options_switches(command_mod) do 34 command_mod.cli_switches 35 |> List.wrap() 36 |> Enum.map(fn 37 %{name: name, type: type} when is_binary(name) -> {String.to_atom(name), type} 38 %{name: name, type: type} when is_atom(name) -> {name, type} 39 end) 40 end 41 42 defp cli_options_aliases(command_mod) do 43 command_mod.cli_switches 44 |> List.wrap() 45 |> Enum.map(fn 46 %{name: name, alias: alias} when is_binary(name) -> {alias, String.to_atom(name)} 47 %{name: name, alias: alias} when is_atom(name) -> {alias, name} 48 _ -> nil 49 end) 50 |> Enum.reject(&is_nil/1) 51 end 52 end