2016-08-18 21:56:21 +08:00
|
|
|
## 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.
|
2019-01-20 11:10:59 +08:00
|
|
|
## Copyright (c) 2007-2019 Pivotal Software, Inc. All rights reserved.
|
2016-08-18 21:56:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
defmodule RabbitMQ.CLI.DefaultOutput do
|
2016-09-05 18:06:45 +08:00
|
|
|
# When `use RabbitMQ.CLI.DefaultOutput` is invoked,
|
2016-11-10 22:53:59 +08:00
|
|
|
# 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
|
2017-05-24 19:08:03 +08:00
|
|
|
def output(result, _opts) do
|
|
|
|
RabbitMQ.CLI.DefaultOutput.output(result)
|
2016-08-18 21:56:21 +08:00
|
|
|
end
|
2016-08-22 19:58:37 +08:00
|
|
|
end
|
|
|
|
end
|
2016-08-18 21:56:21 +08:00
|
|
|
|
2017-05-24 19:08:03 +08:00
|
|
|
def output(result) do
|
2016-08-22 19:58:37 +08:00
|
|
|
result
|
|
|
|
|> normalize_output()
|
2017-05-24 19:08:03 +08:00
|
|
|
|> format_output()
|
2016-08-22 19:58:37 +08:00
|
|
|
end
|
2016-08-18 21:56:21 +08:00
|
|
|
|
2016-08-22 19:58:37 +08:00
|
|
|
def mnesia_running_error(node_name) do
|
|
|
|
"Mnesia is still running on node #{node_name}.\n" <>
|
2017-08-12 08:00:14 +08:00
|
|
|
"Please stop RabbitMQ with 'rabbitmqctl stop_app' first."
|
2016-08-22 19:58:37 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
defp normalize_output(:ok), do: :ok
|
|
|
|
defp normalize_output({:ok, _} = input), do: input
|
2017-04-05 02:23:50 +08:00
|
|
|
defp normalize_output({:stream, _} = input), do: input
|
2017-06-16 19:41:11 +08:00
|
|
|
defp normalize_output({:badrpc_multi, _, _} = input), do: {:error, input}
|
2017-05-24 19:08:03 +08:00
|
|
|
defp normalize_output({:badrpc, :nodedown} = input), do: {:error, input}
|
|
|
|
defp normalize_output({:badrpc, :timeout} = input), do: {:error, input}
|
2017-08-05 03:41:12 +08:00
|
|
|
defp normalize_output({:badrpc, {:EXIT, reason}}), do: {:error, reason}
|
2016-11-07 22:08:26 +08:00
|
|
|
defp normalize_output({:error, format, args})
|
2016-11-13 14:29:17 +08:00
|
|
|
when (is_list(format) or is_binary(format)) and is_list(args) do
|
2017-05-24 19:08:03 +08:00
|
|
|
{:error, to_string(:rabbit_misc.format(format, args))}
|
|
|
|
end
|
|
|
|
defp normalize_output({:error_string, string}) do
|
|
|
|
{:error, to_string(string)}
|
2016-11-07 22:08:26 +08:00
|
|
|
end
|
2016-08-22 19:58:37 +08:00
|
|
|
defp normalize_output({:error, _} = 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}
|
|
|
|
|
2017-05-24 19:08:03 +08:00
|
|
|
defp format_output({:error, _} = result) do
|
|
|
|
result
|
2016-08-22 19:58:37 +08:00
|
|
|
end
|
2017-05-24 19:08:03 +08:00
|
|
|
defp format_output(:ok) do
|
2016-08-22 19:58:37 +08:00
|
|
|
:ok
|
|
|
|
end
|
2017-05-24 19:08:03 +08:00
|
|
|
defp format_output({:ok, output}) do
|
2016-08-22 19:58:37 +08:00
|
|
|
case Enumerable.impl_for(output) do
|
2017-06-01 23:32:23 +08:00
|
|
|
nil ->
|
|
|
|
{:ok, output};
|
2016-12-07 01:14:59 +08:00
|
|
|
## Do not streamify plain maps
|
2017-06-01 23:32:23 +08:00
|
|
|
Enumerable.Map ->
|
|
|
|
{:ok, output};
|
|
|
|
## Do not streamify keyword lists
|
|
|
|
Enumerable.List ->
|
|
|
|
case Keyword.keyword?(output) do
|
|
|
|
true -> {:ok, output};
|
|
|
|
false -> {:stream, output}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
{:stream, output}
|
2016-08-18 21:56:21 +08:00
|
|
|
end
|
|
|
|
end
|
2017-05-24 19:08:03 +08:00
|
|
|
defp format_output({:stream, stream}) do
|
2017-04-05 02:23:50 +08:00
|
|
|
{:stream, stream}
|
|
|
|
end
|
2016-08-18 21:56:21 +08:00
|
|
|
end
|