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-11-01 02:07:24 +08:00
|
|
|
##
|
2023-11-22 12:18:22 +08:00
|
|
|
## Copyright (c) 2007-2025 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
|
2016-11-01 02:07:24 +08:00
|
|
|
|
2018-07-23 13:06:18 +08:00
|
|
|
alias RabbitMQ.CLI.Formatters.FormatterHelpers
|
2016-11-15 19:14:44 +08:00
|
|
|
|
2016-11-01 02:07:24 +08:00
|
|
|
defmodule RabbitMQ.CLI.Formatters.Table do
|
2016-11-15 01:52:08 +08:00
|
|
|
@behaviour RabbitMQ.CLI.FormatterBehaviour
|
2016-11-01 02:07:24 +08:00
|
|
|
|
2019-01-31 05:02:11 +08:00
|
|
|
def switches(), do: [table_headers: :boolean, pad_to_header: :boolean]
|
2019-01-24 07:04:12 +08:00
|
|
|
|
2016-11-10 02:18:19 +08:00
|
|
|
def format_stream(stream, options) do
|
2018-10-24 21:43:58 +08:00
|
|
|
# Flatten for list_consumers
|
2019-01-31 03:20:29 +08:00
|
|
|
Stream.flat_map(
|
|
|
|
stream,
|
|
|
|
fn
|
|
|
|
[first | _] = element ->
|
2019-05-23 22:48:02 +08:00
|
|
|
case FormatterHelpers.proplist?(first) or is_map(first) do
|
2023-04-24 18:49:23 +08:00
|
|
|
true -> FormatterHelpers.to_predictably_ordered_keyword_list(element)
|
2019-01-31 03:20:29 +08:00
|
|
|
false -> [element]
|
|
|
|
end
|
|
|
|
|
|
|
|
other ->
|
|
|
|
[other]
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|> Stream.transform(
|
|
|
|
:init,
|
|
|
|
FormatterHelpers.without_errors_2(fn
|
|
|
|
element, :init ->
|
|
|
|
{maybe_header(element, options), :next}
|
|
|
|
|
|
|
|
element, :next ->
|
|
|
|
{[format_output_1(element, options)], :next}
|
|
|
|
end)
|
|
|
|
)
|
2016-11-10 02:18:19 +08:00
|
|
|
end
|
|
|
|
|
2023-04-24 18:49:23 +08:00
|
|
|
def format_output(output0, options) do
|
|
|
|
# on Erlang 26, map entry ordering has changed, this avoids
|
|
|
|
# implicitly depending on map key order
|
|
|
|
output = FormatterHelpers.to_predictably_ordered_keyword_list(output0)
|
2018-10-24 21:43:58 +08:00
|
|
|
maybe_header(output, options)
|
2018-10-22 17:55:16 +08:00
|
|
|
end
|
|
|
|
|
2018-10-24 21:43:58 +08:00
|
|
|
defp maybe_header(output, options) do
|
2018-11-16 19:51:48 +08:00
|
|
|
opt_table_headers = Map.get(options, :table_headers, true)
|
|
|
|
opt_silent = Map.get(options, :silent, false)
|
2019-01-31 03:20:29 +08:00
|
|
|
|
2018-11-16 19:51:48 +08:00
|
|
|
case {opt_silent, opt_table_headers} do
|
|
|
|
{true, _} ->
|
|
|
|
[format_output_1(output, options)]
|
2019-01-31 03:20:29 +08:00
|
|
|
|
2018-11-16 19:51:48 +08:00
|
|
|
{false, false} ->
|
|
|
|
[format_output_1(output, options)]
|
2019-01-31 03:20:29 +08:00
|
|
|
|
2018-11-16 19:51:48 +08:00
|
|
|
{false, true} ->
|
|
|
|
format_header(output) ++ [format_output_1(output, options)]
|
2018-10-24 21:43:58 +08:00
|
|
|
end
|
2018-10-22 17:55:16 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
defp format_output_1(output, options) when is_map(output) do
|
2016-11-04 20:38:35 +08:00
|
|
|
escaped = escaped?(options)
|
2019-01-24 07:04:12 +08:00
|
|
|
pad_to_header = pad_to_header?(options)
|
2023-04-24 18:49:23 +08:00
|
|
|
|
2019-01-24 07:04:12 +08:00
|
|
|
format_line(output, escaped, pad_to_header)
|
2016-11-01 02:07:24 +08:00
|
|
|
end
|
2019-01-31 03:20:29 +08:00
|
|
|
|
2018-10-22 17:55:16 +08:00
|
|
|
defp format_output_1([], _) do
|
2016-11-10 02:18:19 +08:00
|
|
|
""
|
|
|
|
end
|
2019-01-31 03:20:29 +08:00
|
|
|
|
|
|
|
defp format_output_1(output, options) do
|
2016-11-04 20:38:35 +08:00
|
|
|
escaped = escaped?(options)
|
2019-01-24 07:04:12 +08:00
|
|
|
pad_to_header = pad_to_header?(options)
|
2019-01-31 03:20:29 +08:00
|
|
|
|
2019-05-23 22:48:02 +08:00
|
|
|
case FormatterHelpers.proplist?(output) do
|
2019-01-31 03:20:29 +08:00
|
|
|
true -> format_line(output, escaped, pad_to_header)
|
|
|
|
false -> format_inspect(output)
|
2016-11-01 02:07:24 +08:00
|
|
|
end
|
|
|
|
end
|
2016-11-04 20:38:35 +08:00
|
|
|
|
|
|
|
defp escaped?(_), do: true
|
|
|
|
|
2019-01-24 07:04:12 +08:00
|
|
|
defp pad_to_header?(%{pad_to_header: pad}), do: pad
|
|
|
|
defp pad_to_header?(_), do: false
|
|
|
|
|
|
|
|
defp format_line(line, escaped, pad_to_header) do
|
2019-01-31 03:20:29 +08:00
|
|
|
values =
|
|
|
|
Enum.map(
|
|
|
|
line,
|
|
|
|
fn {k, v} ->
|
|
|
|
line = FormatterHelpers.format_info_item(v, escaped)
|
|
|
|
|
|
|
|
case pad_to_header do
|
|
|
|
true ->
|
|
|
|
String.pad_trailing(
|
|
|
|
to_string(line),
|
|
|
|
String.length(to_string(k))
|
|
|
|
)
|
|
|
|
|
|
|
|
false ->
|
|
|
|
line
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2016-11-01 02:07:24 +08:00
|
|
|
Enum.join(values, "\t")
|
|
|
|
end
|
|
|
|
|
|
|
|
defp format_inspect(output) do
|
2016-11-04 20:38:35 +08:00
|
|
|
case is_binary(output) do
|
2019-01-31 03:20:29 +08:00
|
|
|
true -> output
|
2016-11-01 02:07:24 +08:00
|
|
|
false -> inspect(output)
|
|
|
|
end
|
|
|
|
end
|
2018-10-22 17:55:16 +08:00
|
|
|
|
|
|
|
@spec format_header(term()) :: [String.t()]
|
|
|
|
defp format_header(output) do
|
2019-01-31 03:20:29 +08:00
|
|
|
keys =
|
|
|
|
case output do
|
|
|
|
map when is_map(map) ->
|
|
|
|
Map.keys(map)
|
|
|
|
|
|
|
|
keyword when is_list(keyword) ->
|
2019-05-23 22:48:02 +08:00
|
|
|
case FormatterHelpers.proplist?(keyword) do
|
2019-01-31 03:20:29 +08:00
|
|
|
true -> Keyword.keys(keyword)
|
|
|
|
false -> []
|
|
|
|
end
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
2018-10-22 17:55:16 +08:00
|
|
|
case keys do
|
|
|
|
[] -> []
|
2019-01-31 03:20:29 +08:00
|
|
|
_ -> [Enum.join(keys, "\t")]
|
2018-10-22 17:55:16 +08:00
|
|
|
end
|
|
|
|
end
|
2016-11-13 14:29:17 +08:00
|
|
|
end
|