diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/validators.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/validators.ex index 598f1fd80c..cbef3a1643 100644 --- a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/validators.ex +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/validators.ex @@ -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 diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/disable_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/disable_command.ex index 3dce4ac115..255e4ce4d5 100644 --- a/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/disable_command.ex +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/disable_command.ex @@ -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 |--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); diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/enable_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/enable_command.ex index 4366b8e046..4ef0b62fad 100644 --- a/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/enable_command.ex +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/enable_command.ex @@ -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 |--all [--offline] [--online]" diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/list_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/list_command.ex index f70f9de53e..b9b7786158 100644 --- a/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/list_command.ex +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/list_command.ex @@ -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]" diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/set_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/set_command.ex index b2c57187af..471dde0bf8 100644 --- a/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/set_command.ex +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/set_command.ex @@ -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 [] [--offline] [--online]" diff --git a/deps/rabbitmq_cli/lib/rabbitmqctl.ex b/deps/rabbitmq_cli/lib/rabbitmqctl.ex index dc8170d81f..cd3e8b5301 100644 --- a/deps/rabbitmq_cli/lib/rabbitmqctl.ex +++ b/deps/rabbitmq_cli/lib/rabbitmqctl.ex @@ -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) diff --git a/deps/rabbitmq_cli/test/plugins/disable_plugins_command_test.exs b/deps/rabbitmq_cli/test/plugins/disable_plugins_command_test.exs index 600bfcc811..32f4593c04 100644 --- a/deps/rabbitmq_cli/test/plugins/disable_plugins_command_test.exs +++ b/deps/rabbitmq_cli/test/plugins/disable_plugins_command_test.exs @@ -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 diff --git a/deps/rabbitmq_cli/test/plugins/enable_plugins_command_test.exs b/deps/rabbitmq_cli/test/plugins/enable_plugins_command_test.exs index e0fd7216a2..5654e6f92a 100644 --- a/deps/rabbitmq_cli/test/plugins/enable_plugins_command_test.exs +++ b/deps/rabbitmq_cli/test/plugins/enable_plugins_command_test.exs @@ -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 diff --git a/deps/rabbitmq_cli/test/plugins/list_plugins_command_test.exs b/deps/rabbitmq_cli/test/plugins/list_plugins_command_test.exs index c1c9d52ce9..3cdefb01c8 100644 --- a/deps/rabbitmq_cli/test/plugins/list_plugins_command_test.exs +++ b/deps/rabbitmq_cli/test/plugins/list_plugins_command_test.exs @@ -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 diff --git a/deps/rabbitmq_cli/test/plugins/set_plugins_command_test.exs b/deps/rabbitmq_cli/test/plugins/set_plugins_command_test.exs index 534452401b..7bf2fb67aa 100644 --- a/deps/rabbitmq_cli/test/plugins/set_plugins_command_test.exs +++ b/deps/rabbitmq_cli/test/plugins/set_plugins_command_test.exs @@ -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 diff --git a/deps/rabbitmq_cli/test/rabbitmqctl_test.exs b/deps/rabbitmq_cli/test/rabbitmqctl_test.exs index 769ce90b2c..2cdf37ae19 100644 --- a/deps/rabbitmq_cli/test/rabbitmqctl_test.exs +++ b/deps/rabbitmq_cli/test/rabbitmqctl_test.exs @@ -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