2016-11-15 19:14:44 +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.
|
2019-01-20 11:10:59 +08:00
|
|
|
## Copyright (c) 2007-2019 Pivotal Software, Inc. All rights reserved.
|
2016-11-15 19:14:44 +08:00
|
|
|
|
2018-07-23 13:06:18 +08:00
|
|
|
alias RabbitMQ.CLI.Formatters.FormatterHelpers
|
2016-11-15 19:14:44 +08:00
|
|
|
|
|
|
|
defmodule RabbitMQ.CLI.Formatters.Csv do
|
|
|
|
@behaviour RabbitMQ.CLI.FormatterBehaviour
|
|
|
|
|
|
|
|
def format_stream(stream, _) do
|
|
|
|
## Flatten list_consumers
|
2019-01-31 03:20:29 +08:00
|
|
|
Stream.flat_map(
|
|
|
|
stream,
|
|
|
|
fn
|
|
|
|
[first | _] = element ->
|
|
|
|
case Keyword.keyword?(first) or is_map(first) do
|
|
|
|
true -> element
|
|
|
|
false -> [element]
|
|
|
|
end
|
|
|
|
|
|
|
|
other ->
|
|
|
|
[other]
|
|
|
|
end
|
|
|
|
)
|
2016-11-15 19:14:44 +08:00
|
|
|
## Add info_items names
|
2019-01-31 03:20:29 +08:00
|
|
|
|> Stream.transform(
|
|
|
|
:init,
|
|
|
|
FormatterHelpers.without_errors_2(fn
|
|
|
|
element, :init ->
|
|
|
|
{
|
|
|
|
case keys(element) do
|
|
|
|
nil -> [values(element)]
|
|
|
|
ks -> [ks, values(element)]
|
|
|
|
end,
|
|
|
|
:next
|
|
|
|
}
|
|
|
|
|
|
|
|
element, :next ->
|
|
|
|
{[values(element)], :next}
|
|
|
|
end)
|
|
|
|
)
|
|
|
|
|> CSV.encode(delimiter: "")
|
2016-11-15 19:14:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def format_output(output, _) do
|
|
|
|
case keys(output) do
|
2019-01-31 03:20:29 +08:00
|
|
|
nil -> [values(output)]
|
|
|
|
ks -> [ks, values(output)]
|
2016-11-15 19:14:44 +08:00
|
|
|
end
|
2019-01-31 03:20:29 +08:00
|
|
|
|> CSV.encode()
|
|
|
|
|> Enum.join()
|
2016-11-15 19:14:44 +08:00
|
|
|
end
|
|
|
|
|
2018-11-01 18:22:56 +08:00
|
|
|
defp keys(map) when is_map(map) do
|
2016-11-15 19:14:44 +08:00
|
|
|
Map.keys(map)
|
|
|
|
end
|
2019-01-31 03:20:29 +08:00
|
|
|
|
2018-11-01 18:22:56 +08:00
|
|
|
defp keys(list) when is_list(list) do
|
2016-11-15 19:14:44 +08:00
|
|
|
case Keyword.keyword?(list) do
|
2019-01-31 03:20:29 +08:00
|
|
|
true -> Keyword.keys(list)
|
2016-11-15 19:14:44 +08:00
|
|
|
false -> nil
|
|
|
|
end
|
|
|
|
end
|
2019-01-31 03:20:29 +08:00
|
|
|
|
2018-11-01 18:22:56 +08:00
|
|
|
defp keys(_other) do
|
2016-11-15 19:14:44 +08:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2018-11-01 18:22:56 +08:00
|
|
|
defp values(map) when is_map(map) do
|
2016-11-15 19:14:44 +08:00
|
|
|
Map.values(map)
|
|
|
|
end
|
2019-01-31 03:20:29 +08:00
|
|
|
|
2018-11-01 18:22:56 +08:00
|
|
|
defp values([]) do
|
2016-11-15 19:14:44 +08:00
|
|
|
[]
|
|
|
|
end
|
2019-01-31 03:20:29 +08:00
|
|
|
|
2018-11-01 18:22:56 +08:00
|
|
|
defp values(list) when is_list(list) do
|
2016-11-15 19:14:44 +08:00
|
|
|
case Keyword.keyword?(list) do
|
2019-01-31 03:20:29 +08:00
|
|
|
true -> Keyword.values(list)
|
2016-11-15 19:14:44 +08:00
|
|
|
false -> list
|
|
|
|
end
|
|
|
|
end
|
2019-01-31 03:20:29 +08:00
|
|
|
|
2018-11-01 18:22:56 +08:00
|
|
|
defp values(other) do
|
2016-11-15 19:14:44 +08:00
|
|
|
other
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defimpl CSV.Encode, for: PID do
|
|
|
|
def encode(pid, env \\ []) do
|
2016-11-15 19:31:59 +08:00
|
|
|
FormatterHelpers.format_info_item(pid)
|
|
|
|
|> to_string
|
|
|
|
|> CSV.Encode.encode(env)
|
2016-11-15 19:14:44 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defimpl CSV.Encode, for: List do
|
2016-11-15 19:31:59 +08:00
|
|
|
def encode(list, env \\ []) do
|
|
|
|
FormatterHelpers.format_info_item(list)
|
|
|
|
|> to_string
|
2016-11-15 19:14:44 +08:00
|
|
|
|> CSV.Encode.encode(env)
|
|
|
|
end
|
2016-11-15 19:31:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
defimpl CSV.Encode, for: Tuple do
|
|
|
|
def encode(tuple, env \\ []) do
|
|
|
|
FormatterHelpers.format_info_item(tuple)
|
|
|
|
|> to_string
|
|
|
|
|> CSV.Encode.encode(env)
|
2016-11-15 19:14:44 +08:00
|
|
|
end
|
|
|
|
end
|
2016-11-15 19:31:59 +08:00
|
|
|
|
|
|
|
defimpl CSV.Encode, for: Map do
|
|
|
|
def encode(map, env \\ []) do
|
|
|
|
FormatterHelpers.format_info_item(map)
|
|
|
|
|> to_string
|
|
|
|
|> CSV.Encode.encode(env)
|
|
|
|
end
|
|
|
|
end
|