2020-07-12 02:23:07 +08:00
|
|
|
## This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
## License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
## file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
2016-10-26 23:25:39 +08:00
|
|
|
##
|
2024-05-09 11:33:36 +08:00
|
|
|
## Copyright (c) 2007-2025 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
|
2018-07-23 13:06:18 +08:00
|
|
|
alias RabbitMQ.CLI.Formatters.FormatterHelpers
|
2016-10-26 23:25:39 +08:00
|
|
|
|
|
|
|
defmodule RabbitMQ.CLI.Formatters.Plugins do
|
2016-11-15 01:52:08 +08:00
|
|
|
@behaviour RabbitMQ.CLI.FormatterBehaviour
|
2016-10-26 23:25:39 +08:00
|
|
|
|
2019-01-31 03:20:29 +08:00
|
|
|
def format_output(
|
|
|
|
%{status: status, format: format, plugins: plugins},
|
|
|
|
options
|
|
|
|
) do
|
2016-10-26 23:25:39 +08:00
|
|
|
legend(status, format, options) ++ format_plugins(plugins, format)
|
|
|
|
end
|
2019-01-31 03:20:29 +08:00
|
|
|
|
2016-10-27 01:11:35 +08:00
|
|
|
def format_output(%{enabled: enabled, mode: _} = output, options) do
|
|
|
|
case length(enabled) do
|
2019-01-31 03:20:29 +08:00
|
|
|
0 ->
|
|
|
|
["Plugin configuration unchanged."]
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
[
|
|
|
|
"The following plugins have been enabled:"
|
|
|
|
| for plugin <- enabled do
|
|
|
|
" #{plugin}"
|
|
|
|
end
|
|
|
|
] ++
|
|
|
|
[""] ++
|
|
|
|
applying(output, options) ++
|
|
|
|
log_offline(output)
|
2016-10-27 01:11:35 +08:00
|
|
|
end
|
|
|
|
end
|
2019-01-31 03:20:29 +08:00
|
|
|
|
2016-10-27 01:11:35 +08:00
|
|
|
def format_output(%{disabled: disabled, mode: _} = output, options) do
|
|
|
|
case length(disabled) do
|
2019-01-31 03:20:29 +08:00
|
|
|
0 ->
|
|
|
|
["Plugin configuration unchanged."]
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
[
|
|
|
|
"The following plugins have been disabled:"
|
|
|
|
| for plugin <- disabled do
|
|
|
|
" #{plugin}"
|
|
|
|
end
|
|
|
|
] ++
|
|
|
|
[""] ++
|
|
|
|
applying(output, options) ++
|
|
|
|
log_offline(output)
|
2016-10-27 01:11:35 +08:00
|
|
|
end
|
|
|
|
end
|
2019-01-31 03:20:29 +08:00
|
|
|
|
2016-12-07 00:18:54 +08:00
|
|
|
## Do not print enabled/disabled for set command
|
|
|
|
def format_output(%{} = output, options) do
|
|
|
|
applying(output, options)
|
2016-10-27 01:11:35 +08:00
|
|
|
end
|
2019-01-31 03:20:29 +08:00
|
|
|
|
2016-12-06 23:12:47 +08:00
|
|
|
def format_output([], %{node: node}) do
|
2019-01-31 03:20:29 +08:00
|
|
|
["All plugins have been disabled.", "Applying plugin configuration to #{node}..."]
|
2016-12-06 23:12:47 +08:00
|
|
|
end
|
2019-01-31 03:20:29 +08:00
|
|
|
|
2016-12-06 23:12:47 +08:00
|
|
|
def format_output(plugins, %{node: node}) when is_list(plugins) do
|
2019-01-31 03:20:29 +08:00
|
|
|
[
|
|
|
|
"The following plugins have been configured:"
|
|
|
|
| for plugin <- plugins do
|
|
|
|
" #{plugin}"
|
|
|
|
end
|
|
|
|
] ++
|
2016-12-06 23:12:47 +08:00
|
|
|
["Applying plugin configuration to #{node}..."]
|
|
|
|
end
|
2019-01-31 03:20:29 +08:00
|
|
|
|
2016-12-06 23:12:47 +08:00
|
|
|
def format_output(output, _) do
|
|
|
|
:io_lib.format("~p", [output])
|
|
|
|
|> to_string
|
|
|
|
end
|
2016-10-26 23:25:39 +08:00
|
|
|
|
|
|
|
def format_stream(stream, options) do
|
2019-01-31 03:20:29 +08:00
|
|
|
Stream.map(
|
|
|
|
stream,
|
|
|
|
FormatterHelpers.without_errors_1(fn element ->
|
|
|
|
format_output(element, options)
|
|
|
|
end)
|
|
|
|
)
|
2016-10-26 23:25:39 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
defp format_plugins(plugins, format) do
|
2019-01-31 03:20:29 +08:00
|
|
|
max_name_length =
|
|
|
|
Enum.reduce(plugins, 0, fn %{name: name}, len ->
|
|
|
|
max(String.length(to_string(name)), len)
|
|
|
|
end)
|
|
|
|
|
2016-10-26 23:25:39 +08:00
|
|
|
for plugin <- plugins do
|
|
|
|
format_plugin(plugin, format, max_name_length)
|
|
|
|
end
|
2019-01-31 03:20:29 +08:00
|
|
|
|> List.flatten()
|
2016-10-26 23:25:39 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
defp format_plugin(%{name: name}, :minimal, _) do
|
|
|
|
to_string(name)
|
|
|
|
end
|
2019-01-31 03:20:29 +08:00
|
|
|
|
2016-10-26 23:25:39 +08:00
|
|
|
defp format_plugin(plugin, :normal, max_name_length) do
|
|
|
|
[summary(plugin) <> inline_version(plugin, max_name_length)]
|
|
|
|
end
|
2019-01-31 03:20:29 +08:00
|
|
|
|
2016-10-26 23:25:39 +08:00
|
|
|
defp format_plugin(plugin, :verbose, _) do
|
|
|
|
[summary(plugin) | verbose(plugin)]
|
|
|
|
end
|
|
|
|
|
|
|
|
defp summary(%{name: name, enabled: enabled, running: running}) do
|
2019-01-31 03:20:29 +08:00
|
|
|
enabled_sign =
|
|
|
|
case enabled do
|
|
|
|
:implicit -> "e"
|
|
|
|
:enabled -> "E"
|
|
|
|
:not_enabled -> " "
|
|
|
|
end
|
|
|
|
|
|
|
|
running_sign =
|
|
|
|
case running do
|
|
|
|
true -> "*"
|
|
|
|
false -> " "
|
|
|
|
end
|
2016-10-26 23:25:39 +08:00
|
|
|
|
|
|
|
"[#{enabled_sign}#{running_sign}] #{name}"
|
|
|
|
end
|
|
|
|
|
2016-12-01 00:19:13 +08:00
|
|
|
defp inline_version(%{name: name} = plugin, max_name_length) do
|
2019-01-31 03:20:29 +08:00
|
|
|
spacing =
|
|
|
|
String.duplicate(
|
|
|
|
" ",
|
|
|
|
max_name_length -
|
|
|
|
String.length(to_string(name))
|
|
|
|
)
|
|
|
|
|
2016-11-30 00:33:41 +08:00
|
|
|
spacing <> " " <> augment_version(plugin)
|
2016-10-26 23:25:39 +08:00
|
|
|
end
|
|
|
|
|
2019-01-31 03:20:29 +08:00
|
|
|
defp verbose(%{dependencies: dependencies, description: description} = plugin) do
|
2016-10-26 23:25:39 +08:00
|
|
|
prettified = to_string(:io_lib.format("~p", [dependencies]))
|
2019-01-31 03:20:29 +08:00
|
|
|
|
2016-10-26 23:25:39 +08:00
|
|
|
[
|
2016-11-30 00:33:41 +08:00
|
|
|
" Version: \t#{augment_version(plugin)}",
|
2016-10-26 23:25:39 +08:00
|
|
|
" Dependencies:\t#{prettified}",
|
|
|
|
" Description: \t#{description}"
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2024-05-08 08:33:19 +08:00
|
|
|
defp augment_version(%{version: version, running: false}) do
|
2016-11-30 00:33:41 +08:00
|
|
|
to_string(version)
|
|
|
|
end
|
|
|
|
|
2024-05-09 08:03:29 +08:00
|
|
|
defp augment_version(%{version: version, running_version: nil}) do
|
|
|
|
to_string(version)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp augment_version(%{version: version, running_version: ""}) do
|
|
|
|
to_string(version)
|
|
|
|
end
|
|
|
|
|
2016-11-30 00:33:41 +08:00
|
|
|
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
|
|
|
|
|
2019-03-31 06:33:36 +08:00
|
|
|
## Do not print legend in minimal, quiet or silent mode
|
2016-10-26 23:25:39 +08:00
|
|
|
defp legend(_, :minimal, _) do
|
|
|
|
[]
|
|
|
|
end
|
2022-10-03 01:54:11 +08:00
|
|
|
|
2019-03-31 06:33:36 +08:00
|
|
|
defp legend(_, _, %{quiet: true}) do
|
|
|
|
[]
|
|
|
|
end
|
2022-10-03 01:54:11 +08:00
|
|
|
|
2019-03-31 06:33:36 +08:00
|
|
|
defp legend(_, _, %{silent: true}) do
|
|
|
|
[]
|
|
|
|
end
|
2019-01-31 03:20:29 +08:00
|
|
|
|
2016-10-26 23:25:39 +08:00
|
|
|
defp legend(status, _, %{node: node}) do
|
2019-01-31 03:20:29 +08:00
|
|
|
[
|
|
|
|
" Configured: E = explicitly enabled; e = implicitly enabled",
|
|
|
|
" | Status: #{status_message(status, node)}",
|
|
|
|
" |/"
|
|
|
|
]
|
2016-10-26 23:25:39 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
defp status_message(:running, node) do
|
|
|
|
"* = running on #{node}"
|
|
|
|
end
|
2019-01-31 03:20:29 +08:00
|
|
|
|
2016-10-26 23:25:39 +08:00
|
|
|
defp status_message(:node_down, node) do
|
|
|
|
"[failed to contact #{node} - status not shown]"
|
|
|
|
end
|
|
|
|
|
2018-02-14 01:08:59 +08:00
|
|
|
defp applying(%{mode: :offline, set: set_plugins}, _) do
|
2019-01-31 03:20:29 +08:00
|
|
|
set_plugins_message =
|
|
|
|
case length(set_plugins) do
|
|
|
|
0 -> "nothing to do"
|
|
|
|
len -> "set #{len} plugins"
|
|
|
|
end
|
|
|
|
|
2018-02-14 01:08:59 +08:00
|
|
|
[set_plugins_message <> "."]
|
|
|
|
end
|
2019-01-31 03:20:29 +08:00
|
|
|
|
2017-06-12 21:32:40 +08:00
|
|
|
defp applying(%{mode: :offline, enabled: enabled}, _) do
|
2019-01-31 03:20:29 +08:00
|
|
|
enabled_message =
|
|
|
|
case length(enabled) do
|
|
|
|
0 -> "nothing to do"
|
|
|
|
len -> "enabled #{len} plugins"
|
|
|
|
end
|
|
|
|
|
2017-06-12 21:32:40 +08:00
|
|
|
[enabled_message <> "."]
|
|
|
|
end
|
2019-01-31 03:20:29 +08:00
|
|
|
|
2017-06-12 21:32:40 +08:00
|
|
|
defp applying(%{mode: :offline, disabled: disabled}, _) do
|
2019-01-31 03:20:29 +08:00
|
|
|
disabled_message =
|
|
|
|
case length(disabled) do
|
|
|
|
0 -> "nothing to do"
|
|
|
|
len -> "disabled #{len} plugins"
|
|
|
|
end
|
|
|
|
|
2017-06-12 21:32:40 +08:00
|
|
|
[disabled_message <> "."]
|
2016-10-27 01:11:35 +08:00
|
|
|
end
|
2019-01-31 03:20:29 +08:00
|
|
|
|
2016-12-06 23:12:47 +08:00
|
|
|
defp applying(%{mode: :online, started: started, stopped: stopped}, _) do
|
2019-01-31 03:20:29 +08:00
|
|
|
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
|
|
|
|
|
2016-12-07 00:18:54 +08:00
|
|
|
[change_message <> "."]
|
2016-10-27 01:11:35 +08:00
|
|
|
end
|
|
|
|
|
2017-06-12 21:32:40 +08:00
|
|
|
defp log_offline(%{mode: :offline}) do
|
|
|
|
["Offline change; changes will take effect at broker restart."]
|
|
|
|
end
|
2019-01-31 03:20:29 +08:00
|
|
|
|
2017-06-12 21:32:40 +08:00
|
|
|
defp log_offline(_) do
|
|
|
|
[]
|
|
|
|
end
|
2016-10-29 00:14:34 +08:00
|
|
|
end
|