summaryrefslogtreecommitdiff
path: root/deps/rabbitmq_cli/lib/rabbitmq/cli/default_output.ex
blob: 603f46e374d82481ef5fe6464c52e69a66143b37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
## 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.
## Copyright (c) 2007-2019 Pivotal Software, Inc.  All rights reserved.

defmodule RabbitMQ.CLI.DefaultOutput do
  # When `use RabbitMQ.CLI.DefaultOutput` is invoked,
  # this will define output/2 that delegates to RabbitMQ.CLI.DefaultOutput.output/3.
  defmacro __using__(_) do
    quote do
      def output(result, _opts) do
        RabbitMQ.CLI.DefaultOutput.output(result)
      end
    end
  end

  def output(result) do
    result
    |> normalize_output()
    |> format_output()
  end

  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
  defp normalize_output({:stream, _} = input), do: input
  defp normalize_output({:badrpc_multi, _, _} = input), do: {:error, input}
  defp normalize_output({:badrpc, :nodedown} = input), do: {:error, input}
  defp normalize_output({:badrpc, :timeout} = input), do: {:error, input}
  defp normalize_output({:badrpc, {:EXIT, reason}}), do: {:error, reason}

  defp normalize_output({:error, format, args})
       when (is_list(format) or is_binary(format)) and is_list(args) do
    {:error, to_string(:rabbit_misc.format(format, args))}
  end

  defp normalize_output({:error_string, string}) do
    {:error, to_string(string)}
  end

  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}

  defp format_output({:error, _} = result) do
    result
  end

  defp format_output(:ok) do
    :ok
  end

  defp format_output({:ok, output}) do
    case Enumerable.impl_for(output) do
      nil ->
        {:ok, output}

      ## Do not streamify plain maps
      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}
    end
  end

  defp format_output({:stream, stream}) do
    {:stream, stream}
  end
end