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
|
## 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/.
##
## Copyright (c) 2007-2022 VMware, Inc. or its affiliates. All rights reserved.
defmodule RabbitMQ.CLI.Ctl.InfoKeys do
import RabbitCommon.Records
alias RabbitMQ.CLI.Core.DataCoercion
def validate_info_keys(args, valid_keys) do
info_keys = prepare_info_keys(args)
case invalid_info_keys(info_keys, Enum.map(valid_keys, &DataCoercion.to_atom/1)) do
[_ | _] = bad_info_keys ->
{:validation_failure, {:bad_info_key, bad_info_keys}}
[] ->
{:ok, info_keys}
end
end
def prepare_info_keys(args) do
args
|> Enum.flat_map(fn arg -> String.split(arg, ",", trim: true) end)
|> Enum.map(fn s -> String.replace(s, ",", "") end)
|> Enum.map(&String.trim/1)
|> Enum.map(&String.to_atom/1)
|> Enum.uniq()
end
def with_valid_info_keys(args, valid_keys, fun) do
case validate_info_keys(args, valid_keys) do
{:ok, info_keys} -> fun.(info_keys)
err -> err
end
end
defp invalid_info_keys(info_keys, valid_keys) do
MapSet.new(info_keys)
|> MapSet.difference(MapSet.new(valid_keys))
|> MapSet.to_list()
end
def info_for_keys(item, []) do
item
end
def info_for_keys([{_, _} | _] = item, info_keys) do
item
|> Enum.filter(fn {k, _} -> Enum.member?(info_keys, k) end)
|> Enum.map(fn {k, v} -> {k, format_info_item(v)} end)
end
defp format_info_item(resource(name: name)) do
name
end
defp format_info_item(any) do
any
end
end
|