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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
## 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-2023 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
defmodule RabbitMQ.CLI.Core.Parser do
alias RabbitMQ.CLI.{CommandBehaviour, FormatterBehaviour}
alias RabbitMQ.CLI.Core.{CommandModules, Config, Helpers}
def default_switches() do
[
node: :string,
quiet: :boolean,
silent: :boolean,
dry_run: :boolean,
vhost: :string,
# for backwards compatibility,
# not all commands support timeouts
timeout: :integer,
longnames: :boolean,
formatter: :string,
printer: :string,
file: :string,
script_name: :string,
rabbitmq_home: :string,
data_dir: :string,
plugins_dir: :string,
enabled_plugins_file: :string,
aliases_file: :string,
erlang_cookie: :string,
help: :boolean,
print_stacktrace: :boolean
]
end
@atomized_options [:node, :script_name, :erlang_cookie]
def default_aliases() do
[
p: :vhost,
n: :node,
q: :quiet,
s: :silent,
l: :longnames,
# for backwards compatibility,
# not all commands support timeouts
t: :timeout,
# For backwards compatibility, `mnesia_dir` was renamed to `data_dir`.
mnesia_dir: :data_dir,
"?": :help
]
end
@spec parse([String.t()]) ::
{command :: :no_command | atom() | {:suggest, String.t()}, command_name :: String.t(),
arguments :: [String.t()], options :: map(),
invalid :: [{String.t(), String.t() | nil}]}
def parse(input) do
{parsed_args, options, invalid} = parse_global(input)
{command_name, command_module, arguments} = look_up_command(parsed_args, options)
case command_module do
nil ->
{:no_command, command_name, arguments, options, invalid}
{:suggest, _} = suggest ->
{suggest, command_name, arguments, options, invalid}
{:alias, alias_module, alias_content} ->
{[_alias_command_name | cmd_arguments], cmd_options, cmd_invalid} =
parse_alias(input, command_name, alias_module, alias_content, options)
{alias_module, command_name, cmd_arguments,
Helpers.atomize_values(cmd_options, @atomized_options), cmd_invalid}
command_module when is_atom(command_module) ->
{[^command_name | cmd_arguments], cmd_options, cmd_invalid} =
parse_command_specific(input, command_module, options)
{command_module, command_name, cmd_arguments,
Helpers.atomize_values(cmd_options, @atomized_options), cmd_invalid}
end
end
def command_suggestion(_cmd_name, empty) when empty == %{} do
nil
end
def command_suggestion(typed, module_map) do
RabbitMQ.CLI.AutoComplete.suggest_command(typed, module_map)
end
defp look_up_command(parsed_args, options) do
case parsed_args do
[cmd_name | arguments] ->
## This is an optimisation for pluggable command discovery.
## Most of the time a command will be from rabbitmqctl application
## so there is not point in scanning plugins for potential commands
_ = CommandModules.load_core(options)
core_commands = CommandModules.module_map_core()
command =
case core_commands[cmd_name] do
nil ->
_ = CommandModules.load(options)
module_map = CommandModules.module_map()
module_map[cmd_name] ||
command_alias(cmd_name, module_map, options) ||
command_suggestion(cmd_name, module_map)
c ->
c
end
{cmd_name, command, arguments}
[] ->
{"", nil, []}
end
end
defp command_alias(cmd_name, module_map, options) do
aliases = load_aliases(options)
case aliases[cmd_name] do
nil ->
nil
[alias_cmd_name | _] = alias_content ->
case module_map[alias_cmd_name] do
nil -> nil
module -> {:alias, module, alias_content}
end
end
end
defp load_aliases(options) do
aliases_file = Config.get_option(:aliases_file, options)
case aliases_file && File.read(aliases_file) do
## No aliases file
nil ->
%{}
{:ok, content} ->
String.split(content, "\n")
|> Enum.reduce(
%{},
fn str, acc ->
case String.split(str, "=", parts: 2) do
[alias_name, alias_string] ->
Map.put(acc, String.trim(alias_name), OptionParser.split(alias_string))
_ ->
acc
end
end
)
{:error, err} ->
IO.puts(:stderr, "Error reading aliases file #{aliases_file}: #{err}")
%{}
end
end
defp parse_alias(input, command_name, module, alias_content, options) do
{pre_command_options, tail, invalid} = parse_global_head(input)
[^command_name | other] = tail
aliased_input = alias_content ++ other
{args, options, command_invalid} = parse_command_specific(aliased_input, module, options)
merged_options = Map.merge(options, pre_command_options)
{args, merged_options, command_invalid ++ invalid}
end
def parse_command_specific(input, command, options \\ %{}) do
formatter = Config.get_formatter(command, options)
switches = build_switches(default_switches(), command, formatter)
aliases = build_aliases(default_aliases(), command, formatter)
parse_generic(input, switches, aliases)
end
def parse_global_head(input) do
switches = default_switches()
aliases = default_aliases()
{options, tail, invalid} =
OptionParser.parse_head(
input,
strict: switches,
aliases: aliases,
allow_nonexistent_atoms: true
)
norm_options = normalize_options(options, switches) |> Map.new()
{norm_options, tail, invalid}
end
def parse_global(input) do
switches = default_switches()
aliases = default_aliases()
parse_generic(input, switches, aliases)
end
defp parse_generic(input, switches, aliases) do
{options, args, invalid} =
OptionParser.parse(
input,
strict: switches,
aliases: aliases,
allow_nonexistent_atoms: true
)
norm_options = normalize_options(options, switches) |> Map.new()
{args, Helpers.atomize_values(norm_options, @atomized_options), invalid}
end
defp build_switches(default, command, formatter) do
command_switches = CommandBehaviour.switches(command)
formatter_switches = FormatterBehaviour.switches(formatter)
assert_no_conflict(
command,
command_switches,
formatter_switches,
:redefining_formatter_switches
)
merge_if_different(
default,
formatter_switches,
{:formatter_invalid,
{formatter, {:redefining_global_switches, default, formatter_switches}}}
)
|> merge_if_different(
command_switches,
{:command_invalid, {command, {:redefining_global_switches, default, command_switches}}}
)
end
defp assert_no_conflict(command, command_fields, formatter_fields, err) do
_ =
merge_if_different(
formatter_fields,
command_fields,
{:command_invalid, {command, {err, formatter_fields, command_fields}}}
)
:ok
end
defp build_aliases(default, command, formatter) do
command_aliases = CommandBehaviour.aliases(command)
formatter_aliases = FormatterBehaviour.aliases(formatter)
assert_no_conflict(command, command_aliases, formatter_aliases, :redefining_formatter_aliases)
merge_if_different(
default,
formatter_aliases,
{:formatter_invalid, {command, {:redefining_global_aliases, default, formatter_aliases}}}
)
|> merge_if_different(
command_aliases,
{:command_invalid, {command, {:redefining_global_aliases, default, command_aliases}}}
)
end
defp merge_if_different(default, specific, error) do
case keyword_intersect(default, specific) do
[] ->
Keyword.merge(default, specific)
conflicts ->
# if all conflicting keys are of the same type,
# that's acceptable
case Enum.all?(
conflicts,
fn c ->
Keyword.get(default, c) == Keyword.get(specific, c)
end
) do
true -> Keyword.merge(default, specific)
false -> exit(error)
end
end
end
defp keyword_intersect(one, two) do
one_keys = MapSet.new(Keyword.keys(one))
two_keys = MapSet.new(Keyword.keys(two))
intersection = MapSet.intersection(one_keys, two_keys)
case Enum.empty?(intersection) do
true -> []
false -> MapSet.to_list(intersection)
end
end
defp normalize_options(options, switches) do
Enum.map(
options,
fn {key, option} ->
{key, normalize_type(option, switches[key])}
end
)
end
defp normalize_type(value, :atom) when is_binary(value) do
String.to_atom(value)
end
defp normalize_type(value, _type) do
value
end
end
|