Make validate/2 and validate_execution_environment/2 use the same tuple tag

Per discussion with @hairyhum.
This commit is contained in:
Michael Klishin 2017-08-09 01:11:14 +03:00
parent afa605b3e9
commit 0031c4fdf6
11 changed files with 37 additions and 56 deletions

View File

@ -31,18 +31,6 @@ defmodule RabbitMQ.CLI.Core.Validators do
def chain([], _) do
:ok
end
def chain([validator | rest], args, error_key) do
case apply(validator, args) do
:ok -> chain(rest, args, error_key)
{:ok, _} -> chain(rest, args, error_key)
{:validation_failure, err} -> {error_key, err}
{:error, err} -> {error_key, err}
end
end
def chain([], _, _) do
:ok
end
def node_is_not_running(_, %{node: node_name}) do
case Helpers.node_running?(node_name) do

View File

@ -50,8 +50,7 @@ defmodule RabbitMQ.CLI.Plugins.Commands.DisableCommand do
&Helpers.require_rabbit_and_plugins/2,
&PluginHelpers.enabled_plugins_file/2,
&Helpers.plugins_dir/2],
[args, opts],
:environment_validation_failure)
[args, opts])
end
def usage, do: "disable <plugin>|--all [--offline] [--online]"
@ -63,7 +62,6 @@ defmodule RabbitMQ.CLI.Plugins.Commands.DisableCommand do
["Disabling plugins on node #{node_name}:" | plugins]
end
def run(plugin_names, %{all: all_flag, node: node_name} = opts) do
plugins = case all_flag do
false -> for s <- plugin_names, do: String.to_atom(s);

View File

@ -50,8 +50,7 @@ defmodule RabbitMQ.CLI.Plugins.Commands.EnableCommand do
&Helpers.require_rabbit_and_plugins/2,
&PluginHelpers.enabled_plugins_file/2,
&Helpers.plugins_dir/2],
[args, opts],
:environment_validation_failure)
[args, opts])
end
def usage, do: "enable <plugin>|--all [--offline] [--online]"

View File

@ -55,8 +55,7 @@ defmodule RabbitMQ.CLI.Plugins.Commands.ListCommand do
&Helpers.require_rabbit_and_plugins/2,
&PluginHelpers.enabled_plugins_file/2,
&Helpers.plugins_dir/2],
[args, opts],
:environment_validation_failure)
[args, opts])
end
def usage, do: "list [pattern] [--verbose] [--minimal] [--enabled] [--implicitly-enabled]"

View File

@ -43,8 +43,7 @@ defmodule RabbitMQ.CLI.Plugins.Commands.SetCommand do
&Helpers.require_rabbit_and_plugins/2,
&PluginHelpers.enabled_plugins_file/2,
&Helpers.plugins_dir/2],
[args, opts],
:environment_validation_failure)
[args, opts])
end
def usage, do: "set [<plugin>] [--offline] [--online]"

View File

@ -77,13 +77,15 @@ defmodule RabbitMQCtl do
:ok ->
# then optionally validate execution environment
case maybe_validate_execution_environment(command, arguments, options) do
:ok -> proceed_to_execution(command, arguments, options)
other -> other
:ok ->
result = proceed_to_execution(command, arguments, options)
handle_command_output(result, command, options, output_fun);
{:validation_failure, err} ->
argument_validation_error_output(err, command, unparsed_command, options)
end
other -> other
# handle_command_output will handle all the errors
# among other things
end |> handle_command_output(command, options, unparsed_command, output_fun)
{:validation_failure, err} ->
environment_validation_error_output(err, command, unparsed_command, options)
end
end)
end
end
@ -113,16 +115,12 @@ defmodule RabbitMQCtl do
end
end
defp handle_command_output(output, command, options, unparsed_command, output_fun) do
def handle_command_output(output, command, options, output_fun) do
case output do
{:error, _, _} = err ->
err;
{:error, _} = err ->
format_error(err, options, command);
{:validation_failure, err} ->
argument_validation_error_output(err, command, unparsed_command, options);
{:environment_validation_failure, err} ->
environment_validation_error_output(err, command, unparsed_command, options);
_ ->
output_fun.(output, command, options)
end
@ -252,13 +250,13 @@ defmodule RabbitMQCtl do
end
end
defp argument_validation_error_output(err_detail, command, unparsed_command, options) do
def argument_validation_error_output(err_detail, command, unparsed_command, options) do
err = format_validation_error(err_detail)
base_error = "Error (argument validation): #{err}\nArguments given:\n\t#{unparsed_command |> Enum.join(" ")}"
validation_error_output(err_detail, base_error, command, options)
end
defp environment_validation_error_output(err_detail, command, unparsed_command, options) do
def environment_validation_error_output(err_detail, command, unparsed_command, options) do
err = format_validation_error(err_detail)
base_error = "Error: #{err}\nArguments given:\n\t#{unparsed_command |> Enum.join(" ")}"
validation_error_output(err_detail, base_error, command, options)

View File

@ -78,12 +78,12 @@ defmodule DisablePluginsCommandTest do
test "validate_execution_environment: not specifying an enabled_plugins_file is reported as an error", context do
assert @command.validate_execution_environment(["a"], Map.delete(context[:opts], :enabled_plugins_file)) ==
{:environment_validation_failure, :no_plugins_file}
{:validation_failure, :no_plugins_file}
end
test "validate_execution_environment: not specifying a plugins_dir is reported as an error", context do
assert @command.validate_execution_environment(["a"], Map.delete(context[:opts], :plugins_dir)) ==
{:environment_validation_failure, :no_plugins_dir}
{:validation_failure, :no_plugins_dir}
end
@ -93,11 +93,11 @@ defmodule DisablePluginsCommandTest do
test "validate_execution_environment: specifying a non-existent plugins_dir is reported as an error", context do
assert @command.validate_execution_environment(["a"], Map.merge(context[:opts], %{plugins_dir: "none"})) ==
{:environment_validation_failure, :plugins_dir_does_not_exist}
{:validation_failure, :plugins_dir_does_not_exist}
end
test "validate_execution_environment: failure to load the rabbit application is reported as an error", context do
assert {:environment_validation_failure, {:unable_to_load_rabbit, _}} =
assert {:validation_failure, {:unable_to_load_rabbit, _}} =
@command.validate_execution_environment(["a"], Map.delete(context[:opts], :rabbitmq_home))
end

View File

@ -78,12 +78,12 @@ defmodule EnablePluginsCommandTest do
test "validate_execution_environment: not specifying an enabled_plugins_file is reported as an error", context do
assert @command.validate_execution_environment(["a"], Map.delete(context[:opts], :enabled_plugins_file)) ==
{:environment_validation_failure, :no_plugins_file}
{:validation_failure, :no_plugins_file}
end
test "validate_execution_environment: not specifying a plugins_dir is reported as an error", context do
assert @command.validate_execution_environment(["a"], Map.delete(context[:opts], :plugins_dir)) ==
{:environment_validation_failure, :no_plugins_dir}
{:validation_failure, :no_plugins_dir}
end
@ -93,11 +93,11 @@ defmodule EnablePluginsCommandTest do
test "validate_execution_environment: specifying a non-existent plugins_dir is reported as an error", context do
assert @command.validate_execution_environment(["a"], Map.merge(context[:opts], %{plugins_dir: "none"})) ==
{:environment_validation_failure, :plugins_dir_does_not_exist}
{:validation_failure, :plugins_dir_does_not_exist}
end
test "validate: failure to load the rabbit application is reported as an error", context do
assert {:environment_validation_failure, {:unable_to_load_rabbit, _}} =
assert {:validation_failure, {:unable_to_load_rabbit, _}} =
@command.validate_execution_environment(["a"], Map.delete(context[:opts], :rabbitmq_home))
end

View File

@ -82,12 +82,12 @@ defmodule ListPluginsCommandTest do
test "validate_execution_environment: not specifying enabled_plugins_file is reported as an error", context do
assert @command.validate_execution_environment(["a"], Map.delete(context[:opts], :enabled_plugins_file)) ==
{:environment_validation_failure, :no_plugins_file}
{:validation_failure, :no_plugins_file}
end
test "validate_execution_environment: not specifying plugins_dir is reported as an error", context do
assert @command.validate_execution_environment(["a"], Map.delete(context[:opts], :plugins_dir)) ==
{:environment_validation_failure, :no_plugins_dir}
{:validation_failure, :no_plugins_dir}
end
@ -97,11 +97,11 @@ defmodule ListPluginsCommandTest do
test "validate_execution_environment: specifying non existent plugins_dir is reported as an error", context do
assert @command.validate_execution_environment(["a"], Map.merge(context[:opts], %{plugins_dir: "none"})) ==
{:environment_validation_failure, :plugins_dir_does_not_exist}
{:validation_failure, :plugins_dir_does_not_exist}
end
test "validate_execution_environment: failure to load rabbit application is reported as an error", context do
assert {:environment_validation_failure, {:unable_to_load_rabbit, _}} =
assert {:validation_failure, {:unable_to_load_rabbit, _}} =
@command.validate_execution_environment(["a"], Map.delete(context[:opts], :rabbitmq_home))
end

View File

@ -71,12 +71,12 @@ defmodule SetPluginsCommandTest do
test "validate_execution_environment: not specifying enabled_plugins_file is reported as an error", context do
assert @command.validate_execution_environment([], Map.delete(context[:opts], :enabled_plugins_file)) ==
{:environment_validation_failure, :no_plugins_file}
{:validation_failure, :no_plugins_file}
end
test "validate_execution_environment: not specifying plugins_dir is reported as an error", context do
assert @command.validate_execution_environment([], Map.delete(context[:opts], :plugins_dir)) ==
{:environment_validation_failure, :no_plugins_dir}
{:validation_failure, :no_plugins_dir}
end
test "validate_execution_environment: specifying a non-existent enabled_plugins_file is fine", context do
@ -86,11 +86,11 @@ defmodule SetPluginsCommandTest do
test "validate_execution_environment: specifying non existent plugins_dir is reported as an error", context do
assert @command.validate_execution_environment([], Map.merge(context[:opts], %{plugins_dir: "none"})) ==
{:environment_validation_failure, :plugins_dir_does_not_exist}
{:validation_failure, :plugins_dir_does_not_exist}
end
test "validate_execution_environment: failure to load rabbit application is reported as an error", context do
assert {:environment_validation_failure, {:unable_to_load_rabbit, _}} =
assert {:validation_failure, {:unable_to_load_rabbit, _}} =
@command.validate_execution_environment([], Map.delete(context[:opts], :rabbitmq_home))
end

View File

@ -188,7 +188,7 @@ defmodule RabbitMQCtlTest do
{:error, ^exit_code, message} =
RabbitMQCtl.handle_command_output(
{:error, {:badrpc, :nodedown}},
:no_command, %{node: node}, [],
:no_command, %{node: node},
fn(output, _, _) -> output end)
assert message =~ ~r/Error: unable to perform an operation on node/
@ -199,7 +199,7 @@ defmodule RabbitMQCtlTest do
{:error, ^exit_code, message} =
RabbitMQCtl.handle_command_output(
{:error, {:badrpc, :nodedown}},
:no_command, %{node: localnode}, [],
:no_command, %{node: localnode},
fn(output, _, _) -> output end)
assert message =~ ~r/DIAGNOSTICS/
assert message =~ ~r/attempted to contact/
@ -214,7 +214,7 @@ defmodule RabbitMQCtlTest do
{:error, ^exit_code, ^err_msg} =
RabbitMQCtl.handle_command_output(
{:error, {:badrpc, :timeout}},
ExampleCommand,%{timeout: timeout, node: nodename}, ["example"],
ExampleCommand, %{timeout: timeout, node: nodename},
fn(output, _, _) -> output end)
end
@ -223,7 +223,7 @@ defmodule RabbitMQCtlTest do
{:error, ^exit_code, "Error:\nerror message"} =
RabbitMQCtl.handle_command_output(
{:error, "error message"},
:no_command, %{}, [],
:no_command, %{},
fn(output, _, _) -> output end)
end
@ -234,7 +234,7 @@ defmodule RabbitMQCtlTest do
{:error, ^exit_code, "Error:\n" <> ^inspected} =
RabbitMQCtl.handle_command_output(
{:error, error},
:no_command, %{}, [],
:no_command, %{},
fn(output, _, _) -> output end)
end
@ -243,7 +243,7 @@ defmodule RabbitMQCtlTest do
{:error, ^exit_code, "Error:\nerror_message"} =
RabbitMQCtl.handle_command_output(
{:error, :error_message},
:no_command, %{}, [],
:no_command, %{},
fn(output, _, _) -> output end)
end