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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
## 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.
alias RabbitMQ.CLI.CommandBehaviour
defmodule RabbitMQ.CLI.Ctl.Commands.HelpCommand do
alias RabbitMQ.CLI.Core.{CommandModules, Config, ExitCodes}
alias RabbitMQ.CLI.Core.CommandModules
import RabbitMQ.CLI.Core.ANSI
@behaviour RabbitMQ.CLI.CommandBehaviour
def scopes(), do: [:ctl, :diagnostics, :plugins, :queues, :streams, :vmware, :upgrade]
def switches(), do: [list_commands: :boolean]
def distribution(_), do: :none
use RabbitMQ.CLI.Core.MergesNoDefaults
def validate([], _), do: :ok
def validate([_command], _), do: :ok
def validate(args, _) when length(args) > 1 do
{:validation_failure, :too_many_args}
end
def run([command_name | _], opts) do
_ = CommandModules.load(opts)
module_map = CommandModules.module_map(opts)
case module_map[command_name] do
nil ->
# command not found
# {:error, all_usage(opts)}
case RabbitMQ.CLI.AutoComplete.suggest_command(command_name, module_map) do
{:suggest, suggested} ->
suggest_message =
"\nCommand '#{command_name}' not found. \n" <>
"Did you mean '#{suggested}'? \n"
{:error, ExitCodes.exit_usage(), suggest_message}
nil ->
{:error, ExitCodes.exit_usage(), "\nCommand '#{command_name}' not found."}
end
command ->
{:ok, command_usage(command, opts)}
end
end
def run([], opts) do
_ = CommandModules.load(opts)
case opts[:list_commands] do
true ->
{:ok, commands_description(opts)}
_ ->
{:ok, all_usage(opts)}
end
end
def output({:ok, result}, _) do
{:ok, result}
end
def output({:error, result}, _) do
{:error, ExitCodes.exit_usage(), result}
end
use RabbitMQ.CLI.DefaultOutput
def banner(_, _), do: nil
def help_section(), do: :help
def description(), do: "Displays usage information for a command"
def usage(), do: "help (<command> | [--list-commands])"
def usage_additional() do
[
["--list-commands", "only output a list of discovered commands"]
]
end
#
# Implementation
#
def all_usage(opts) do
tool_name = program_name(opts)
tool_usage(tool_name) ++
["\n\nAvailable commands:\n"] ++
commands_description(opts) ++
help_footer(tool_name)
end
def command_usage(command, opts) do
Enum.join(
[base_usage(command, opts)] ++
command_description(command) ++
additional_usage(command) ++
relevant_doc_guides(command) ++
general_options_usage(),
"\n\n"
) <> "\n"
end
defp tool_usage(tool_name) do
[
"\n#{bright("Usage")}\n\n" <>
"#{tool_name} [--node <node>] [--timeout <timeout>] [--longnames] [--quiet] <command> [<command options>]"
]
end
def base_usage(command, opts) do
tool_name = program_name(opts)
maybe_timeout =
case command_supports_timeout(command) do
true -> " [--timeout <timeout>]"
false -> ""
end
Enum.join([
"\n#{bright("Usage")}\n\n",
"#{tool_name} [--node <node>] [--longnames] [--quiet] " <>
flatten_string(command.usage(), maybe_timeout)
])
end
defp flatten_string(list, additional) when is_list(list) do
list
|> Enum.map(fn line -> line <> additional end)
|> Enum.join("\n")
end
defp flatten_string(str, additional) when is_binary(str) do
str <> additional
end
defp general_options_usage() do
[
"#{bright("General Options")}
The following options are accepted by most or all commands.
short | long | description
-----------------|---------------|--------------------------------
-? | --help | displays command help
-n <node> | --node <node> | connect to node <node>
-l | --longnames | use long host names
-t | --timeout <n> | for commands that support it, operation timeout in seconds
-q | --quiet | suppress informational messages
-s | --silent | suppress informational messages
| and table header row
-p | --vhost | for commands that are scoped to a virtual host,
| | virtual host to use
| --formatter | alternative result formatter to use
| if supported: json, pretty_table, table, csv, erlang
not all commands support all (or any) alternative formatters."
]
end
defp command_description(command) do
case CommandBehaviour.description(command) do
"" -> []
other -> [other <> ".\n"]
end
end
defp list_item_formatter([option, description]) do
"#{option}\n\t#{description}\n"
end
defp list_item_formatter({option, description}) do
"#{option}\n\t#{description}\n"
end
defp list_item_formatter(line) do
"#{line}\n"
end
defp additional_usage(command) do
command_usage =
case CommandBehaviour.usage_additional(command) do
list when is_list(list) -> list |> Enum.map(&list_item_formatter/1)
bin when is_binary(bin) -> ["#{bin}\n"]
end
case command_usage do
[] ->
[]
usage ->
[flatten_string(["#{bright("Arguments and Options")}\n" | usage], "")]
end
end
defp relevant_doc_guides(command) do
guide_list =
case CommandBehaviour.usage_doc_guides(command) do
list when is_list(list) -> list |> Enum.map(fn ln -> " * #{ln}\n" end)
bin when is_binary(bin) -> [" * #{bin}\n"]
end
case guide_list do
[] ->
[]
usage ->
[flatten_string(["#{bright("Relevant Doc Guides")}\n" | usage], "")]
end
end
defp help_footer(tool_name) do
["Use '#{tool_name} help <command>' to learn more about a specific command"]
end
defp command_supports_timeout(command) do
nil != CommandBehaviour.switches(command)[:timeout]
end
defp commands_description(opts) do
module_map = CommandModules.module_map(opts)
pad_commands_to =
Enum.reduce(module_map, 0, fn {name, _}, longest ->
name_length = String.length(name)
case name_length > longest do
true -> name_length
false -> longest
end
end)
lines =
module_map
|> Enum.map(fn {name, cmd} ->
description = CommandBehaviour.description(cmd)
help_section = CommandBehaviour.help_section(cmd)
{name, {description, help_section}}
end)
|> Enum.group_by(fn {_, {_, help_section}} -> help_section end)
|> Enum.sort_by(fn {help_section, _} ->
case help_section do
:deprecated -> 999
:other -> 100
{:plugin, _} -> 99
:help -> 1
:node_management -> 2
:cluster_management -> 3
:replication -> 3
:user_management -> 4
:access_control -> 5
:observability_and_health_checks -> 6
:parameters -> 7
:policies -> 8
:virtual_hosts -> 9
_ -> 98
end
end)
|> Enum.map(fn {help_section, section_helps} ->
[
"\n" <> bright(section_head(help_section)) <> ":\n\n"
| Enum.sort(section_helps)
|> Enum.map(fn {name, {description, _}} ->
" #{String.pad_trailing(name, pad_commands_to)} #{description}\n"
end)
]
end)
|> Enum.concat()
lines ++ ["\n"]
end
defp section_head(help_section) do
case help_section do
:help ->
"Help"
:user_management ->
"Users"
:cluster_management ->
"Cluster"
:replication ->
"Replication"
:node_management ->
"Nodes"
:queues ->
"Queues"
:observability_and_health_checks ->
"Monitoring, observability and health checks"
:virtual_hosts ->
"Virtual hosts"
:access_control ->
"Access Control"
:parameters ->
"Parameters"
:policies ->
"Policies"
:configuration ->
"Configuration and Environment"
:feature_flags ->
"Feature flags"
:other ->
"Other"
{:plugin, plugin} ->
plugin_section(plugin) <> " plugin"
custom ->
snake_case_to_capitalized_string(custom)
end
end
defp strip_rabbitmq_prefix(value, regex) do
Regex.replace(regex, value, "")
end
defp format_known_plugin_name_fragments(value) do
case value do
["amqp1.0"] -> "AMQP 1.0"
["amqp1", "0"] -> "AMQP 1.0"
["management"] -> "Management"
["management", "agent"] -> "Management"
["mqtt"] -> "MQTT"
["stomp"] -> "STOMP"
["web", "mqtt"] -> "Web MQTT"
["web", "stomp"] -> "Web STOMP"
[other] -> snake_case_to_capitalized_string(other)
fragments -> snake_case_to_capitalized_string(Enum.join(fragments, "_"))
end
end
defp plugin_section(plugin) do
regex = Regex.recompile!(~r/^rabbitmq_/)
to_string(plugin)
# drop rabbitmq_
|> strip_rabbitmq_prefix(regex)
|> String.split("_")
|> format_known_plugin_name_fragments()
end
defp snake_case_to_capitalized_string(value) do
to_string(value)
|> String.split("_")
|> Enum.map(&String.capitalize/1)
|> Enum.join(" ")
end
defp program_name(opts) do
Config.get_option(:script_name, opts)
end
end
|