summaryrefslogtreecommitdiff
path: root/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/plugins.ex
blob: 84b6080b5e2911f5c00141131e455741fd9dd3df (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
## 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-2017 Pivotal Software, Inc.  All rights reserved.
alias RabbitMQ.CLI.Formatters.FormatterHelpers

defmodule RabbitMQ.CLI.Formatters.Plugins do
  @behaviour RabbitMQ.CLI.FormatterBehaviour

  def format_output(%{status: status, format: format, plugins: plugins},
                    options) do
    legend(status, format, options) ++ format_plugins(plugins, format)
  end
  def format_output(%{enabled: enabled, mode: _} = output, options) do
    case length(enabled) do
      0 -> ["Plugin configuration unchanged."];
      _ -> ["The following plugins have been enabled:" |
            for plugin <- enabled do "  #{plugin}" end] ++ [""]++
            applying(output, options) ++
            log_offline(output)
    end
  end
  def format_output(%{disabled: disabled, mode: _} = output, options) do
    case length(disabled) do
      0 -> ["Plugin configuration unchanged."];
      _ -> ["The following plugins have been disabled:" |
            for plugin <- disabled do "  #{plugin}" end] ++ [""] ++
            applying(output, options) ++
            log_offline(output)
    end
  end
  ## Do not print enabled/disabled for set command
  def format_output(%{} = output, options) do
    applying(output, options)
  end
  def format_output([], %{node: node}) do
    ["All plugins have been disabled.",
     "Applying plugin configuration to #{node}..."]
  end
  def format_output(plugins, %{node: node}) when is_list(plugins) do
    ["The following plugins have been configured:" |
     for plugin <- plugins do "  #{plugin}" end] ++
      ["Applying plugin configuration to #{node}..."]
  end
  def format_output(output, _) do
    :io_lib.format("~p", [output])
    |> to_string
  end

  def format_stream(stream, options) do
    Stream.map(stream,
      FormatterHelpers.without_errors_1(
        fn(element) ->
            format_output(element, options)
        end))
  end

  defp format_plugins(plugins, format) do
    max_name_length = Enum.reduce(plugins, 0,
                                  fn(%{name: name}, len) ->
                                    max(String.length(to_string(name)), len)
                                  end)
    for plugin <- plugins do
      format_plugin(plugin, format, max_name_length)
    end
    |> List.flatten
  end

  defp format_plugin(%{name: name}, :minimal, _) do
    to_string(name)
  end
  defp format_plugin(plugin, :normal, max_name_length) do
    [summary(plugin) <> inline_version(plugin, max_name_length)]
  end
  defp format_plugin(plugin, :verbose, _) do
    [summary(plugin) | verbose(plugin)]
  end

  defp summary(%{name: name, enabled: enabled, running: running}) do
    enabled_sign = case enabled do
      :implicit -> "e";
      :enabled  -> "E";
      :not_enabled -> " "
    end
    running_sign = case running do
      true  -> "*";
      false -> " "
    end

    "[#{enabled_sign}#{running_sign}] #{name}"
  end

  defp inline_version(%{name: name} = plugin, max_name_length) do
    spacing = String.duplicate(" ", max_name_length -
                                    String.length(to_string(name)))
    spacing <> " " <> augment_version(plugin)
  end

  defp verbose(%{dependencies: dependencies,
                 description: description} = plugin) do
    prettified = to_string(:io_lib.format("~p", [dependencies]))
    [
      "     Version:     \t#{augment_version(plugin)}",
      "     Dependencies:\t#{prettified}",
      "     Description: \t#{description}"
    ]

  end

  defp augment_version(%{version: version, running_version: nil} ) do
    to_string(version)
  end

  defp augment_version(%{version: version, running_version: version}) do
    to_string(version)
  end

  defp augment_version(%{version: version, running_version: running_version}) do
    "#{running_version} (pending upgrade to #{version})"
  end

  ## Do not print legend in minimal mode
  defp legend(_, :minimal, _) do
    []
  end
  defp legend(status, _, %{node: node}) do
    [" Configured: E = explicitly enabled; e = implicitly enabled",
     " | Status: #{status_message(status, node)}",
     " |/"]
  end

  defp status_message(:running, node) do
    "* = running on #{node}"
  end
  defp status_message(:node_down, node) do
    "[failed to contact #{node} - status not shown]"
  end

  defp applying(%{mode: :offline, set: set_plugins}, _) do
    set_plugins_message = case length(set_plugins) do
      0   -> "nothing to do";
      len -> "set #{len} plugins"
    end
    [set_plugins_message <> "."]
  end
  defp applying(%{mode: :offline, enabled: enabled}, _) do
    enabled_message = case length(enabled) do
      0   -> "nothing to do";
      len -> "enabled #{len} plugins"
    end
    [enabled_message <> "."]
  end
  defp applying(%{mode: :offline, disabled: disabled}, _) do
    disabled_message = case length(disabled) do
      0   -> "nothing to do";
      len -> "disabled #{len} plugins"
    end
    [disabled_message <> "."]
  end
  defp applying(%{mode: :online, started: started, stopped: stopped}, _) do
    stopped_message = case length(stopped) do
      0   -> [];
      len -> ["stopped #{len} plugins"]
    end
    started_message = case length(started) do
      0   -> [];
      len -> ["started #{len} plugins"]
    end
    change_message = case Enum.join(started_message ++ stopped_message, " and ") do
      ""  -> "nothing to do";
      msg -> msg
    end
    [change_message <> "."]
  end

  defp log_offline(%{mode: :offline}) do
    ["Offline change; changes will take effect at broker restart."]
  end
  defp log_offline(_) do
    []
  end

end