256 lines
8.3 KiB
Elixir
256 lines
8.3 KiB
Elixir
## 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.
|
|
## Copyright (c) 2007-2017 Pivotal Software, Inc. All rights reserved.
|
|
|
|
|
|
defmodule RabbitMQCtl do
|
|
alias RabbitMQ.CLI.Core.Distribution, as: Distribution
|
|
|
|
alias RabbitMQ.CLI.Ctl.Commands.HelpCommand, as: HelpCommand
|
|
alias RabbitMQ.CLI.Core.Output, as: Output
|
|
alias RabbitMQ.CLI.Core.ExitCodes, as: ExitCodes
|
|
|
|
import RabbitMQ.CLI.Core.Helpers
|
|
import RabbitMQ.CLI.Core.Parser
|
|
|
|
@type options() :: Map.t
|
|
@type command_result() :: {:error, ExitCodes.exit_code, term()} | term()
|
|
|
|
def main(["--auto-complete" | []]) do
|
|
handle_shutdown(:ok)
|
|
end
|
|
def main(["--auto-complete", script_name | args]) do
|
|
script_basename = Path.basename(script_name)
|
|
auto_complete(script_basename, args)
|
|
end
|
|
def main(unparsed_command) do
|
|
unparsed_command
|
|
|> exec_command(fn(command, output, options) ->
|
|
formatter = get_formatter(command, options)
|
|
printer = get_printer(options)
|
|
|
|
output
|
|
|> Output.format_output(formatter, options)
|
|
|> Output.print_output(printer, options)
|
|
end)
|
|
|> handle_shutdown
|
|
end
|
|
|
|
def exec_command(unparsed_command, output_fun) do
|
|
{command, command_name, arguments, parsed_options, invalid} = parse(unparsed_command)
|
|
case {command, invalid} do
|
|
{:no_command, _} ->
|
|
command_not_found_string = case command_name do
|
|
"" -> ""
|
|
_ -> "\nCommand '#{command_name}' not found. \n"
|
|
end
|
|
usage_string = command_not_found_string <>
|
|
HelpCommand.all_usage(parsed_options)
|
|
{:error, ExitCodes.exit_usage, usage_string};
|
|
{{:suggest, suggested}, _} ->
|
|
suggest_message = "\nCommand '#{command_name}' not found. \n"<>
|
|
"Did you mean '#{suggested}'? \n"
|
|
{:error, ExitCodes.exit_usage, suggest_message};
|
|
{_, [_|_]} ->
|
|
|
|
validation_error({:bad_option, invalid}, command,
|
|
unparsed_command, parsed_options);
|
|
_ ->
|
|
options = parsed_options |> merge_all_defaults |> normalize_options
|
|
with_distribution(options, fn() ->
|
|
case execute_command(options, command, arguments) do
|
|
{:error, _, _} = err ->
|
|
err;
|
|
{:validation_failure, err} ->
|
|
validation_error(err, command, unparsed_command, options);
|
|
output ->
|
|
output_fun.(command, output, options)
|
|
end
|
|
end)
|
|
end
|
|
end
|
|
|
|
def handle_shutdown({:error, exit_code, output}) do
|
|
output_device = case exit_code == ExitCodes.exit_ok do
|
|
true -> :stdio;
|
|
false -> :stderr
|
|
end
|
|
for line <- List.flatten([output]) do
|
|
IO.puts(output_device, line)
|
|
end
|
|
exit_program(exit_code)
|
|
end
|
|
def handle_shutdown(_) do
|
|
exit_program(ExitCodes.exit_ok)
|
|
end
|
|
|
|
def auto_complete(script_name, args) do
|
|
Rabbitmq.CLI.AutoComplete.complete(script_name, args)
|
|
|> Stream.map(&IO.puts/1) |> Stream.run
|
|
exit_program(ExitCodes.exit_ok)
|
|
end
|
|
|
|
def merge_all_defaults(%{} = options) do
|
|
options
|
|
|> merge_defaults_node
|
|
|> merge_defaults_timeout
|
|
|> merge_defaults_longnames
|
|
end
|
|
|
|
defp merge_defaults_node(%{} = opts), do: Map.merge(%{node: get_rabbit_hostname()}, opts)
|
|
|
|
defp merge_defaults_timeout(%{} = opts), do: Map.merge(%{timeout: :infinity}, opts)
|
|
|
|
defp merge_defaults_longnames(%{} = opts), do: Map.merge(%{longnames: false}, opts)
|
|
|
|
defp normalize_options(opts) do
|
|
opts
|
|
|> normalize_node
|
|
|> normalize_timeout
|
|
end
|
|
|
|
defp normalize_node(%{node: node} = opts) do
|
|
Map.merge(opts, %{node: parse_node(node)})
|
|
end
|
|
|
|
defp normalize_timeout(%{timeout: timeout} = opts)
|
|
when is_integer(timeout) do
|
|
Map.put(opts, :timeout, timeout * 1000)
|
|
end
|
|
defp normalize_timeout(opts) do
|
|
opts
|
|
end
|
|
|
|
|
|
defp maybe_connect_to_rabbitmq(HelpCommand, _), do: nil
|
|
defp maybe_connect_to_rabbitmq(_, node) do
|
|
connect_to_rabbitmq(node)
|
|
end
|
|
|
|
defp execute_command(options, command, arguments) do
|
|
{arguments, options} = command.merge_defaults(arguments, options)
|
|
case command.validate(arguments, options) do
|
|
:ok ->
|
|
maybe_print_banner(command, arguments, options)
|
|
maybe_connect_to_rabbitmq(command, options[:node])
|
|
maybe_run_command(command, arguments, options)
|
|
{:validation_failure, _} = err -> err
|
|
end
|
|
end
|
|
|
|
defp maybe_run_command(_, _, %{dry_run: true}) do
|
|
:ok
|
|
end
|
|
defp maybe_run_command(command, arguments, options) do
|
|
try do
|
|
command.run(arguments, options) |> command.output(options)
|
|
catch _error_type, error ->
|
|
{:error, ExitCodes.exit_software,
|
|
to_string(:io_lib.format("Error: ~n~p~n Stacktrace ~p~n",
|
|
[error, System.stacktrace()]))}
|
|
end
|
|
end
|
|
|
|
defp get_formatter(command, %{formatter: formatter}) do
|
|
module_name = Module.safe_concat("RabbitMQ.CLI.Formatters", Macro.camelize(formatter))
|
|
case Code.ensure_loaded(module_name) do
|
|
{:module, _} -> module_name;
|
|
{:error, :nofile} -> default_formatter(command)
|
|
end
|
|
end
|
|
defp get_formatter(command, _) do
|
|
default_formatter(command)
|
|
end
|
|
|
|
def get_printer(%{printer: printer}) do
|
|
module_name = String.to_atom("RabbitMQ.CLI.Printers." <> Macro.camelize(printer))
|
|
case Code.ensure_loaded(module_name) do
|
|
{:module, _} -> module_name;
|
|
{:error, :nofile} -> default_printer()
|
|
end
|
|
end
|
|
def get_printer(_) do
|
|
default_printer()
|
|
end
|
|
|
|
def default_printer() do
|
|
RabbitMQ.CLI.Printers.StdIO
|
|
end
|
|
|
|
def default_formatter(command) do
|
|
case function_exported?(command, :formatter, 0) do
|
|
true -> command.formatter;
|
|
false -> RabbitMQ.CLI.Formatters.Inspect
|
|
end
|
|
end
|
|
|
|
## Suppress banner if --quiet option is provided
|
|
defp maybe_print_banner(_, _, %{quiet: true}) do
|
|
nil
|
|
end
|
|
defp maybe_print_banner(command, args, opts) do
|
|
case command.banner(args, opts) do
|
|
nil -> nil
|
|
banner ->
|
|
case banner do
|
|
list when is_list(list) ->
|
|
for line <- list, do: IO.puts(line);
|
|
binary when is_binary(binary) ->
|
|
IO.puts(binary)
|
|
end
|
|
end
|
|
end
|
|
|
|
defp validation_error(err_detail, command, unparsed_command, options) do
|
|
err = format_validation_error(err_detail) # TODO format the error better
|
|
base_error = "Error: #{err}\nGiven:\n\t#{unparsed_command |> Enum.join(" ")}"
|
|
usage = HelpCommand.base_usage(command, options)
|
|
message = base_error <> "\n" <> usage
|
|
{:error, ExitCodes.exit_code_for({:validation_failure, err_detail}), message}
|
|
end
|
|
|
|
defp format_validation_error(:not_enough_args), do: "not enough arguments."
|
|
defp format_validation_error({:not_enough_args, detail}), do: "not enough arguments. #{detail}"
|
|
defp format_validation_error(:too_many_args), do: "too many arguments."
|
|
defp format_validation_error({:too_many_args, detail}), do: "too many arguments. #{detail}"
|
|
defp format_validation_error(:bad_argument), do: "Bad argument."
|
|
defp format_validation_error({:bad_argument, detail}), do: "Bad argument. #{detail}"
|
|
defp format_validation_error({:bad_option, opts}) do
|
|
header = "Invalid options for this command:"
|
|
Enum.join([header | for {key, val} <- opts do "#{key} : #{val}" end], "\n")
|
|
end
|
|
defp format_validation_error(err), do: inspect err
|
|
|
|
defp exit_program(code) do
|
|
:net_kernel.stop
|
|
exit({:shutdown, code})
|
|
end
|
|
|
|
@spec with_distribution(options(), (() -> command_result())) :: command_result()
|
|
defp with_distribution(options, code) do
|
|
# Tries to start net_kernel distribution, and calls `code`
|
|
# function on success. Otherswise returns error suitable for
|
|
# handle_shutdown/0.
|
|
case Distribution.start(options) do
|
|
:ok ->
|
|
code.()
|
|
{:ok, _} ->
|
|
code.()
|
|
{:error, reason} ->
|
|
{:error, ExitCodes.exit_config, "Distribution failed: #{inspect reason}"}
|
|
end
|
|
end
|
|
|
|
end
|