rabbitmq-server/deps/rabbitmq_cli/lib/rabbitmq/cli/default_output.ex

100 lines
3.6 KiB
Elixir
Raw Normal View History

## The contents of this file are subject to the Mozilla Public License
## Version 1.1 (the "License"); you may not use this file except in
## compliance with the License. You may obtain a copy of the License
## at http://www.mozilla.org/MPL/
##
## Software distributed under the License is distributed on an "AS IS"
## basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
## the License for the specific language governing rights and
## limitations under the License.
##
## The Original Code is RabbitMQ.
##
## The Initial Developer of the Original Code is GoPivotal, Inc.
2017-01-10 22:42:40 +08:00
## Copyright (c) 2007-2017 Pivotal Software, Inc. All rights reserved.
defmodule RabbitMQ.CLI.DefaultOutput do
2016-11-15 01:52:08 +08:00
alias RabbitMQ.CLI.Core.ExitCodes, as: ExitCodes
alias RabbitMQ.CLI.Core.CommandModules, as: CommandModules
2016-09-05 18:06:45 +08:00
# When `use RabbitMQ.CLI.DefaultOutput` is invoked,
# this will define output/2 that delegates to RabbitMQ.CLI.DefaultOutput.output/3.
2016-08-22 19:58:37 +08:00
defmacro __using__(_) do
quote do
def output(result, opts) do
RabbitMQ.CLI.DefaultOutput.output(result, opts, __MODULE__)
end
2016-08-22 19:58:37 +08:00
end
end
def output(result, opts, module) do
2016-08-22 19:58:37 +08:00
result
|> normalize_output()
|> format_output(opts, module)
2016-08-22 19:58:37 +08:00
end
2016-08-22 19:58:37 +08:00
def mnesia_running_error(node_name) do
"Mnesia is still running on node #{node_name}.\n" <>
"Please stop RabbitMQ with rabbitmqctl stop_app first."
end
defp normalize_output(:ok), do: :ok
defp normalize_output({:ok, _} = input), do: input
2016-12-23 20:12:33 +08:00
defp normalize_output({:badrpc_multi, err, _}), do: {:badrpc, err}
2016-08-22 19:58:37 +08:00
defp normalize_output({:badrpc, :nodedown} = input), do: input
defp normalize_output({:badrpc, :timeout} = input), do: input
2016-11-07 22:08:26 +08:00
defp normalize_output({:error, format, args})
when (is_list(format) or is_binary(format)) and is_list(args) do
2016-11-07 22:08:26 +08:00
{:error, :rabbit_misc.format(format, args)}
end
2016-08-22 19:58:37 +08:00
defp normalize_output({:error, _} = input), do: input
defp normalize_output({:error_string, _} = input), do: input
defp normalize_output(unknown) when is_atom(unknown), do: {:error, unknown}
defp normalize_output({unknown, _} = input) when is_atom(unknown), do: {:error, input}
defp normalize_output(result) when not is_atom(result), do: {:ok, result}
defp format_output({:badrpc, :nodedown} = result, opts, _) do
diagnostics = get_node_diagnostics(opts[:node])
2016-08-22 19:58:37 +08:00
{:error, ExitCodes.exit_code_for(result),
"Error: unable to connect to node '#{opts[:node]}': nodedown\n" <>
diagnostics}
2016-08-22 19:58:37 +08:00
end
defp format_output({:badrpc, :timeout} = result, opts, module) do
op = CommandModules.module_to_command(module)
2016-08-22 19:58:37 +08:00
{:error, ExitCodes.exit_code_for(result),
"Error: operation #{op} on node #{opts[:node]} timed out. Timeout: #{opts[:timeout]}"}
2016-08-22 19:58:37 +08:00
end
defp format_output({:error, err} = result, _, _) do
2016-08-22 19:58:37 +08:00
string_err = string_or_inspect(err)
{:error, ExitCodes.exit_code_for(result), "Error:\n#{string_err}"}
end
defp format_output({:error_string, error_string}, _, _) do
{:error, ExitCodes.exit_software, to_string(error_string)}
2016-08-22 19:58:37 +08:00
end
defp format_output(:ok, _, _) do
2016-08-22 19:58:37 +08:00
:ok
end
defp format_output({:ok, output}, _, _) do
2016-08-22 19:58:37 +08:00
case Enumerable.impl_for(output) do
2016-12-07 01:14:59 +08:00
nil -> {:ok, output};
## Do not streamify plain maps
Enumerable.Map -> {:ok, output};
_ -> {:stream, output}
end
end
defp get_node_diagnostics(nil) do
"Node is not defined"
end
defp get_node_diagnostics(node_name) do
to_string(:rabbit_nodes.diagnostics([node_name]))
end
2016-08-22 19:58:37 +08:00
defp string_or_inspect(val) do
case String.Chars.impl_for(val) do
nil -> inspect(val);
_ -> to_string(val)
end
end
end