2016-11-01 02:07:24 +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.
|
2018-10-24 21:43:58 +08:00
|
|
|
## Copyright (c) 2007-2018 Pivotal Software, Inc. 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
|
|
|
|
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
|
2016-11-10 02:18:19 +08:00
|
|
|
Stream.flat_map(stream,
|
2017-05-24 20:28:51 +08:00
|
|
|
fn([first | _] = element) ->
|
|
|
|
case Keyword.keyword?(first) or is_map(first) do
|
|
|
|
true -> element;
|
|
|
|
false -> [element]
|
|
|
|
end
|
|
|
|
(other) ->
|
|
|
|
[other]
|
|
|
|
end)
|
2018-10-22 17:55:16 +08:00
|
|
|
|> Stream.transform(:init,
|
|
|
|
FormatterHelpers.without_errors_2(
|
|
|
|
fn(element, :init) ->
|
2018-10-24 21:43:58 +08:00
|
|
|
{maybe_header(element, options), :next}
|
2018-10-22 17:55:16 +08:00
|
|
|
(element, :next) ->
|
|
|
|
{[format_output_1(element, options)], :next}
|
|
|
|
end))
|
2016-11-10 02:18:19 +08:00
|
|
|
end
|
|
|
|
|
2018-10-22 17:55:16 +08:00
|
|
|
def format_output(output, options) do
|
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)
|
|
|
|
case {opt_silent, opt_table_headers} do
|
|
|
|
{true, _} ->
|
|
|
|
[format_output_1(output, options)]
|
|
|
|
{false, false} ->
|
|
|
|
[format_output_1(output, options)]
|
|
|
|
{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)
|
|
|
|
format_line(output, escaped)
|
2016-11-01 02:07:24 +08:00
|
|
|
end
|
2018-10-22 17:55:16 +08:00
|
|
|
defp format_output_1([], _) do
|
2016-11-10 02:18:19 +08:00
|
|
|
""
|
|
|
|
end
|
2018-10-22 17:55:16 +08:00
|
|
|
defp format_output_1(output, options)do
|
2016-11-04 20:38:35 +08:00
|
|
|
escaped = escaped?(options)
|
2016-11-01 02:07:24 +08:00
|
|
|
case Keyword.keyword?(output) do
|
2016-11-04 20:38:35 +08:00
|
|
|
true -> format_line(output, escaped);
|
2017-05-24 20:28:51 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
defp format_line(line, escaped) do
|
2016-11-01 02:07:24 +08:00
|
|
|
values = Enum.map(line,
|
2018-12-29 06:44:16 +08:00
|
|
|
fn({_k, v}) ->
|
|
|
|
FormatterHelpers.format_info_item(v, escaped)
|
2016-11-01 02:07:24 +08:00
|
|
|
end)
|
|
|
|
Enum.join(values, "\t")
|
|
|
|
end
|
|
|
|
|
|
|
|
defp format_inspect(output) do
|
2016-11-04 20:38:35 +08:00
|
|
|
case is_binary(output) do
|
2016-11-01 02:07:24 +08:00
|
|
|
true -> output;
|
|
|
|
false -> inspect(output)
|
|
|
|
end
|
|
|
|
end
|
2018-10-22 17:55:16 +08:00
|
|
|
|
|
|
|
@spec format_header(term()) :: [String.t()]
|
|
|
|
defp format_header(output) do
|
|
|
|
keys = case output do
|
|
|
|
map when is_map(map) -> Map.keys(map);
|
|
|
|
keyword when is_list(keyword) ->
|
|
|
|
case Keyword.keyword?(keyword) do
|
|
|
|
true -> Keyword.keys(keyword)
|
|
|
|
false -> []
|
|
|
|
end
|
|
|
|
_ -> []
|
|
|
|
end
|
|
|
|
case keys do
|
|
|
|
[] -> []
|
|
|
|
_ -> [Enum.join(keys, "\t")]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-13 14:29:17 +08:00
|
|
|
end
|