Aliases in behaviour. List plugins command

This commit is contained in:
Daniil Fedotov 2016-06-20 16:52:36 +01:00
parent dc8912c2dd
commit 75d8632d87
55 changed files with 341 additions and 32 deletions

View File

@ -5,5 +5,6 @@ all:
tests: all
mix test
plugins: all
rm rabbitmq-plugins
ln -s rabbitmqctl rabbitmq-plugins

View File

@ -18,4 +18,5 @@ defmodule RabbitCommon.Records do
import Record, only: [defrecord: 2, extract: 2]
defrecord :amqqueue, extract(:amqqueue, from_lib: "rabbit_common/include/rabbit.hrl")
defrecord :plugin, extract(:plugin, from_lib: "rabbit_common/include/rabbit.hrl")
end

View File

@ -22,4 +22,5 @@ defmodule RabbitMQ.CLI.CommandBehaviour do
@callback banner(List.t, Map.t) :: String.t
@callback run(List.t, Map.t) :: any
@callback switches() :: Keyword.t
@callback aliases() :: Keyword.t
end

View File

@ -15,6 +15,8 @@
defmodule RabbitMQ.CLI.Ctl.CommandModules do
@commands_ns ~r/RabbitMQ.CLI.(.*).Commands/
def module_map do
case Application.get_env(:rabbitmqctl, :commands) do
nil -> load;
@ -41,28 +43,32 @@ defmodule RabbitMQ.CLI.Ctl.CommandModules do
end
defp load_commands(scope) do
modules = loadable_modules()
modules
|> Enum.filter(fn(path) ->
to_string(path) =~ ~r/RabbitMQ.CLI.*.Commands/
ctl_and_plugin_modules
|> Enum.filter(fn(mod) ->
to_string(mod) =~ @commands_ns
and
implements_command_behaviour?(mod)
and
command_in_scope(mod, scope)
end)
|> Enum.map(fn(path) ->
Path.rootname(path, '.beam')
|> String.to_atom
|> Code.ensure_loaded
end)
|> Enum.filter_map(fn({res, _}) -> res == :module end,
fn({_, mod}) -> command_tuple(mod) end)
|> Enum.filter(fn({_, cmd}) -> command_in_scope(cmd, scope) end)
|> Enum.map(&command_tuple/1)
|> Map.new
end
defp loadable_modules do
:code.get_path()
|> Enum.flat_map(fn(path) ->
{:ok, modules} = :erl_prim_loader.list_dir(path)
modules
end)
defp ctl_and_plugin_modules do
# No plugins so far
applications = [:rabbitmqctl]
applications
|> Enum.flat_map(fn(app) -> Application.spec(app, :modules) end)
end
defp implements_command_behaviour?(nil) do
false
end
defp implements_command_behaviour?(module) do
Enum.member?(module.module_info(:attributes)[:behaviour] || [],
RabbitMQ.CLI.CommandBehaviour)
end
defp command_tuple(cmd) do
@ -107,8 +113,20 @@ defmodule RabbitMQ.CLI.Ctl.CommandModules do
false
end
defp command_in_scope(cmd, scope) do
cmd
|> to_string
|> String.contains?("RabbitMQ.CLI.#{scope}.Commands")
Enum.member?(command_scopes(cmd), scope)
end
defp command_scopes(cmd) do
case :erlang.function_exported(cmd, :scopes, 0) do
true ->
cmd.scopes()
false ->
@commands_ns
|> Regex.run(to_string(cmd), capture: :all_but_first)
|> List.first
|> to_snake_case
|> String.to_atom
|> List.wrap
end
end
end

View File

@ -23,6 +23,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.AddUserCommand do
def merge_defaults(args, opts), do: {args, opts}
def switches(), do: []
def aliases(), do: []
def validate(args, _) when length(args) < 2 do
{:validation_failure, :not_enough_args}

View File

@ -25,6 +25,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.AddVhostCommand do
def merge_defaults(args, opts), do: {args, opts}
def switches(), do: []
def aliases(), do: []
def run([vhost], %{node: node_name}) do
:rabbit_misc.rpc_call(node_name, :rabbit_vhost, :add, [vhost])
end

View File

@ -23,6 +23,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.AuthenticateUserCommand do
def validate([_,_], _), do: :ok
def merge_defaults(args, opts), do: {args, opts}
def switches(), do: []
def aliases(), do: []
def run([user, password], %{node: node_name}) do
:rabbit_misc.rpc_call(node_name,

View File

@ -23,6 +23,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.CancelSyncQueueCommand do
def flags, do: [:vhost]
def switches, do: []
def aliases, do: []
def usage, do: "cancel_sync_queue [-p <vhost>] queue"

View File

@ -21,6 +21,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ChangePasswordCommand do
def merge_defaults(args, opts), do: {args, opts}
def switches(), do: []
def aliases(), do: []
def validate(args, _) when length(args) < 2, do: {:validation_failure, :not_enough_args}
def validate([_|_] = args, _) when length(args) > 2, do: {:validation_failure, :too_many_args}

View File

@ -19,6 +19,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ClearParameterCommand do
@flags [:vhost]
def switches(), do: []
def aliases(), do: []
def merge_defaults(args, opts) do
default_opts = Map.merge(opts, %{vhost: "/"})
{args, default_opts}

View File

@ -23,6 +23,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ClearPasswordCommand do
def validate([_], _), do: :ok
def merge_defaults(args, opts), do: {args, opts}
def switches(), do: []
def aliases(), do: []
def run([_user] = args, %{node: node_name}) do
:rabbit_misc.rpc_call(node_name, :rabbit_auth_backend_internal, :clear_password, args)

View File

@ -29,6 +29,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ClearPermissionsCommand do
def validate([_], _), do: :ok
def switches(), do: []
def aliases(), do: []
def run([username], %{node: node_name, vhost: vhost}) do
:rabbit_misc.rpc_call(node_name,

View File

@ -23,6 +23,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.CloseConnectionCommand do
def validate(args, _) when length(args) < 2, do: {:validation_failure, :not_enough_args}
def validate([_,_], _), do: :ok
def switches(), do: []
def aliases(), do: []
def run([pid, explanation], %{node: node_name}) do

View File

@ -20,6 +20,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ClusterStatusCommand do
def merge_defaults(args, opts), do: {args, opts}
def switches(), do: []
def aliases(), do: []
def validate(args, _) when length(args) != 0, do: {:validation_failure, :too_many_args}
def validate([], _), do: :ok

View File

@ -23,6 +23,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.DeleteUserCommand do
def merge_defaults(args, opts), do: {args, opts}
def switches(), do: []
def aliases(), do: []
def run([username], %{node: node_name}) do
:rabbit_misc.rpc_call(node_name,
:rabbit_auth_backend_internal,

View File

@ -23,6 +23,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.DeleteVhostCommand do
def merge_defaults(args, opts), do: {args, opts}
def switches(), do: []
def aliases(), do: []
def run([arg], %{node: node_name}) do
:rabbit_misc.rpc_call(node_name, :rabbit_vhost, :delete, [arg])
end

View File

@ -22,6 +22,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.EnvironmentCommand do
def merge_defaults(args, opts), do: {args, opts}
def switches(), do: []
def aliases(), do: []
def run([], %{node: node_name}) do
:rabbit_misc.rpc_call(node_name, :rabbit, :environment, [])
end

View File

@ -22,6 +22,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ForceResetCommand do
def validate([_|_] = args, _) when length(args) > 0, do: {:validation_failure, :too_many_args}
def validate([], _), do: :ok
def switches(), do: []
def aliases(), do: []
def run([], %{node: node_name}) do

View File

@ -24,6 +24,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.HelpCommand do
def merge_defaults(args, opts), do: {args, opts}
def switches(), do: []
def aliases(), do: []
def run([command_name], _) do
case Helpers.is_command?(command_name) do

View File

@ -31,6 +31,8 @@ defmodule RabbitMQ.CLI.Ctl.Commands.JoinClusterCommand do
]
end
def aliases(), do: []
def merge_defaults(args, opts) do
{args, Map.merge(%{disc: true, ram: false}, opts)}
end

View File

@ -38,6 +38,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ListBindingsCommand do
{args, Map.merge(default_opts, opts)}
end
def switches(), do: []
def aliases(), do: []
def flags() do
[:vhost]

View File

@ -39,6 +39,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ListChannelsCommand do
def merge_defaults(args, opts), do: {args, opts}
def switches(), do: []
def aliases(), do: []
def flags() do
[]

View File

@ -40,6 +40,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ListConnectionsCommand do
def merge_defaults(args, opts), do: {args, opts}
def switches(), do: []
def aliases(), do: []
def flags() do
[]

View File

@ -36,6 +36,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ListConsumersCommand do
def merge_defaults(args, opts), do: {args, opts}
def switches(), do: []
def aliases(), do: []
def flags() do
[:vhost]

View File

@ -34,6 +34,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ListExchangesCommand do
def merge_defaults(args, opts), do: {args, opts}
def switches(), do: []
def aliases(), do: []
def flags() do
[:vhost]

View File

@ -22,6 +22,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ListParametersCommand do
end
def switches(), do: []
def aliases(), do: []
def validate([_|_], _) do
{:validation_failure, :too_many_args}

View File

@ -23,6 +23,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ListPermissionsCommand do
end
def switches(), do: []
def aliases(), do: []
def validate([_|_], _) do
{:validation_failure, :too_many_args}

View File

@ -48,6 +48,8 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ListQueuesCommand do
def switches(), do: [offline: :boolean, online: :boolean]
def aliases(), do: []
def flags() do
[:vhost, :offline, :online]
end

View File

@ -23,6 +23,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ListUserPermissionsCommand do
def merge_defaults(args, opts), do: {args, opts}
def switches(), do: []
def aliases(), do: []
def run([username], %{node: node_name, timeout: time_out}) do
:rabbit_misc.rpc_call(node_name,

View File

@ -20,6 +20,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ListUsersCommand do
def merge_defaults(args, opts), do: {args, opts}
def switches(), do: []
def aliases(), do: []
def validate([_|_], _) do
{:validation_failure, :too_many_args}
end

View File

@ -22,6 +22,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ListVhostsCommand do
@info_keys ~w(name tracing)a
def switches(), do: []
def aliases(), do: []
def validate(args, _) do
case InfoKeys.validate_info_keys(args, @info_keys) do
{:ok, _} -> :ok

View File

@ -23,6 +23,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.NodeHealthCheckCommand do
def merge_defaults(args, opts), do: {args, opts}
def switches(), do: []
def aliases(), do: []
def usage, do: "node_health_check"

View File

@ -20,6 +20,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.PurgeQueueCommand do
def flags, do: []
def switches, do: []
def aliases, do: []
def usage, do: "purge_queue <queue>"
def run([queue], %{node: node_name, vhost: vhost, timeout: timeout}) do

View File

@ -29,6 +29,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ReportCommand do
@flags []
def switches(), do: []
def aliases(), do: []
def merge_defaults(args, opts), do: {args, opts}
def validate([_|_] = args, _) when length(args) != 0, do: {:validation_failure, :too_many_args}

View File

@ -22,6 +22,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ResetCommand do
def validate([_|_] = args, _) when length(args) > 0, do: {:validation_failure, :too_many_args}
def validate([], _), do: :ok
def switches(), do: []
def aliases(), do: []
def run([], %{node: node_name}) do

View File

@ -22,6 +22,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.RotateLogsCommand do
def validate([_|_] = args, _) when length(args) > 0, do: {:validation_failure, :too_many_args}
def validate([], _), do: :ok
def switches(), do: []
def aliases(), do: []
def run([], %{node: node_name}) do

View File

@ -19,6 +19,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.SetClusterNameCommand do
@flags []
def switches(), do: []
def aliases(), do: []
def merge_defaults(args, opts), do: {args, opts}

View File

@ -21,6 +21,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.SetDiskFreeLimitCommand do
@flags []
def merge_defaults(args, opts), do: {args, opts}
def switches(), do: []
def aliases(), do: []
def validate([], _) do
{:validation_failure, :not_enough_args}

View File

@ -19,6 +19,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.SetParameterCommand do
@flags [:vhost]
def switches(), do: []
def aliases(), do: []
def merge_defaults(args, opts) do
default_opts = Map.merge(opts, %{vhost: "/"})
{args, default_opts}

View File

@ -20,6 +20,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.SetPermissionsCommand do
def merge_defaults(args, opts), do: {args, opts}
def switches(), do: []
def aliases(), do: []
def validate([], _) do
{:validation_failure, :not_enough_args}
end

View File

@ -20,6 +20,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.SetUserTagsCommand do
def merge_defaults(args, opts), do: {args, opts}
def switches(), do: []
def aliases(), do: []
def validate([], _), do: {:validation_failure, :not_enough_args}
def validate(_, _), do: :ok
def run([user | tags], %{node: node_name}) do

View File

@ -21,6 +21,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.SetVmMemoryHighWatermarkCommand do
@flags []
def merge_defaults(args, opts), do: {args, opts}
def switches(), do: []
def aliases(), do: []
def validate([], _) do
{:validation_failure, :not_enough_args}

View File

@ -22,6 +22,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.StartAppCommand do
def validate([_|_] = args, _) when length(args) > 0, do: {:validation_failure, :too_many_args}
def validate([], _), do: :ok
def switches(), do: []
def aliases(), do: []
def run([], %{node: node_name}) do

View File

@ -22,6 +22,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.StatusCommand do
def validate([_|_] = args, _) when length(args) > 0, do: {:validation_failure, :too_many_args}
def validate([], _), do: :ok
def switches(), do: []
def aliases(), do: []
def run([], %{node: node_name}) do

View File

@ -22,6 +22,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.StopAppCommand do
def validate([_|_] = args, _) when length(args) > 0, do: {:validation_failure, :too_many_args}
def validate([], _), do: :ok
def switches(), do: []
def aliases(), do: []
def run([], %{node: node_name}) do

View File

@ -22,6 +22,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.StopCommand do
def validate([_|_] = args, _) when length(args) > 0, do: {:validation_failure, :too_many_args}
def validate([], _), do: :ok
def switches(), do: []
def aliases(), do: []
def run([], %{node: node_name}) do

View File

@ -23,6 +23,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.SyncQueueCommand do
def flags, do: [:vhost]
def switches, do: []
def aliases, do: []
def usage, do: "sync_queue [-p <vhost>] queue"

View File

@ -23,6 +23,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.TraceOffCommand do
def validate([_|_], _), do: {:validation_failure, :too_many_args}
def validate(_, _), do: :ok
def switches(), do: []
def aliases(), do: []
def merge_defaults(_, opts) do
{[], Map.merge(opts, %{vhost: @default_vhost})}
end

View File

@ -26,6 +26,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.TraceOnCommand do
end
def merge_defaults(args, opts), do: {args, opts}
def switches(), do: []
def aliases(), do: []
def run([], %{node: node_name, vhost: vhost}) do
:rabbit_misc.rpc_call(node_name, :rabbit_trace, :start, [vhost])

View File

@ -25,6 +25,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.WaitCommand do
def validate([_], _), do: :ok
def switches(), do: []
def aliases(), do: []
def run([pid_file], %{node: node_name}) do
wait_for_application(node_name, pid_file, :rabbit_and_plugins);

View File

@ -28,14 +28,7 @@ defmodule RabbitMQ.CLI.Ctl.Helpers do
def is_command?([]), do: true
def is_command?([head | _]), do: is_command?(head)
def is_command?(str), do: implements_command_behaviour?(commands[str])
defp implements_command_behaviour?(nil) do
false
end
defp implements_command_behaviour?(module) do
[RabbitMQ.CLI.CommandBehaviour] === module.module_info(:attributes)[:behaviour]
end
def is_command?(str), do: commands[str] != nil
def get_rabbit_hostname(), do: ("rabbit@#{hostname}") |> String.to_atom

View File

@ -27,7 +27,8 @@ defmodule RabbitMQ.CLI.Ctl.Parser do
timeout: :integer,
vhost: :string,
longnames: :boolean]),
aliases: [p: :vhost, n: :node, q: :quiet, t: :timeout, l: :longnames]
aliases: build_aliases([p: :vhost, n: :node, q: :quiet,
t: :timeout, l: :longnames])
)
{clear_on_empty_command(cmd), options_map(options), invalid}
end
@ -51,6 +52,25 @@ defmodule RabbitMQ.CLI.Ctl.Parser do
end)
end
defp build_aliases(default) do
Enum.reduce(RabbitMQ.CLI.Ctl.Helpers.commands,
default,
fn({_, _}, {:error, _} = err) -> err;
({_, command}, aliases) ->
command_aliases = command.aliases()
case Enum.filter(command_aliases,
fn({key, val}) ->
existing_val = aliases[key]
existing_val != nil and existing_val != val
end) do
[] -> aliases ++ command_aliases;
_ -> exit({:command_invalid,
{command, {:invalid_switches,
command_aliases}}})
end
end)
end
defp options_map(opts) do
opts
|> Map.new

View File

@ -0,0 +1,158 @@
## 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-2016 Pivotal Software, Inc. All rights reserved.
defmodule RabbitMQ.CLI.Plugins.Commands.ListCommand do
import RabbitCommon.Records
alias RabbitMQ.CLI.Plugins.Helpers, as: PluginHelpers
@behaviour RabbitMQ.CLI.CommandBehaviour
def merge_defaults([], opts), do: merge_defaults([".*"], opts)
def merge_defaults(args, opts), do: {args, Map.merge(default_opts, opts)}
def switches(), do: [verbose: :boolean,
minimal: :boolean,
enabled: :boolean,
'implicitly-enabled': :boolean]
def aliases(), do: [v: :verbose, m: :minimal,
'E': :enabled, e: :'implicitly-enabled']
def validate(args, _) when length(args) > 1 do
{:validation_failure, :too_many_args}
end
def validate(_, %{verbose: true, minimal: true}) do
{:validation_failure, {:bad_argument, "Cannot set both verbose and minimal"}}
end
def validate(_, opts) do
:ok
|> validate_step(fn() -> PluginHelpers.require_rabbit(opts) end)
|> validate_step(fn() -> PluginHelpers.enabled_plugins_file(opts) end)
|> validate_step(fn() -> PluginHelpers.plugins_dist_dir(opts) end)
end
def validate_step(:ok, step) do
case step.() do
{:error, err} -> {:validation_failure, err};
_ -> :ok
end
end
def validate_step({:validation_failure, err}, _) do
{:validation_failure, err}
end
def usage, do: "list [pattern] [--verbose] [--minimal] [--enabled] [--implicitly-enabled]"
def banner([pattern], _), do: "Listing plugins with pattern \"#{pattern}\" ..."
def flags, do: Keyword.keys(switches())
def run([pattern] = args, %{node: node_name} = opts) do
%{verbose: verbose, minimal: minimal,
enabled: only_enabled,
'implicitly-enabled': all_enabled} = opts
all = PluginHelpers.list(opts)
enabled = PluginHelpers.read_enabled(opts)
case enabled -- plugin_names(all) do
[] -> :ok;
missing -> IO.puts("WARNING - plugins currently enabled but missing: #{missing}~n~n")
end
implicit = :rabbit_plugins.dependencies(false, enabled, all)
enabled_implicitly = implicit -- enabled
{status, running} =
case :rabbit_misc.rpc_call(node_name, :rabbit_plugins, :active, []) do
{:badrpc, _} -> {:node_down, []};
active -> {:running, active}
end
{:ok, re} = Regex.compile(pattern)
format = case {verbose, minimal} do
{true, false} -> :verbose;
{false, true} -> :minimal;
{false, false} -> :normal
end
plugins = Enum.filter(all,
fn(plugin) ->
name = plugin_name(plugin)
Regex.match?(re, to_string(name)) and
cond do
only_enabled -> Enum.member?(enabled, name);
all_enabled -> Enum.member(enabled ++ enabled_implicitly, name);
true -> true
end
end)
%{status: status,
plugins: format_plugins(plugins, format, enabled, enabled_implicitly, running)}
end
defp format_plugins(plugins, format, enabled, enabled_implicitly, running) do
plugins
|> sort_plugins
|> Enum.map(fn(plugin) ->
format_plugin(plugin, format, enabled, enabled_implicitly, running)
end)
end
defp sort_plugins(plugins) do
Enum.sort_by(plugins, &plugin_name/1)
end
defp format_plugin(plugin, :minimal, _, _, _) do
plugin_name(plugin)
end
defp format_plugin(plugin, :normal, enabled, enabled_implicitly, running) do
plugin(name: name, version: version) = plugin
enabled_mode = case {Enum.member?(enabled, name), Enum.member?(enabled_implicitly, name)} do
{true, false} -> :enabled;
{false, true} -> :implicit;
{false, false} -> :not_enabled
end
%{name: name,
version: version,
enabled: enabled_mode,
running: Enum.member?(running, name)}
end
defp format_plugin(plugin, :verbose, enabled, enabled_implicitly, running) do
normal = format_plugin(plugin, :normal, enabled, enabled_implicitly, running)
plugin(dependencies: dependencies, description: description) = plugin
Map.merge(normal, %{dependencies: dependencies, description: description})
end
defp plugin_names(plugins) do
for plugin <- plugins, do: plugin_name(plugin)
end
defp plugin_name(plugin) do
plugin(name: name) = plugin
name
end
defp default_opts() do
%{minimal: false, verbose: false,
enabled: false, 'implicitly-enabled': false}
end
end

View File

@ -0,0 +1,69 @@
## 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-2016 Pivotal Software, Inc. All rights reserved.
defmodule RabbitMQ.CLI.Plugins.Helpers do
def list(opts) do
{:ok, dir} = plugins_dist_dir(opts)
add_all_to_path(dir)
:rabbit_plugins.list(String.to_char_list(dir))
end
def read_enabled(opts) do
{:ok, enabled} = enabled_plugins_file(opts)
:rabbit_plugins.read_enabled(String.to_char_list(enabled))
end
def enabled_plugins_file(opts) do
case opts[:enabled_plugins_file] || System.get_env("RABBITMQ_ENABLED_PLUGINS_FILE") do
nil -> {:error, :no_plugins_file};
file ->
case File.exists?(file) do
true -> {:ok, file};
false -> {:error, :plugins_file_not_exists}
end
end
end
def plugins_dist_dir(opts) do
case opts[:plugins_dist_dir] || System.get_env("RABBITMQ_PLUGINS_DIR") do
nil -> {:error, :no_plugins_dir};
dir ->
case File.dir?(dir) do
true -> {:ok, dir};
false -> {:error, :plugins_dist_dir_not_exists}
end
end
end
def require_rabbit(opts) do
home = opts[:rabbitmq_home] || System.get_env("RABBITMQ_HOME")
path = :filename.join(home, "ebin")
Code.append_path(path)
case Application.load(:rabbit) do
:ok -> :ok;
{:error, {:already_loaded, :rabbit}} -> :ok;
{:error, err} -> {:error, {:unable_to_load_rabbit, err}}
end
end
defp add_all_to_path(dir) do
{:ok, subdirs} = File.ls(dir)
for subdir <- subdirs do
Path.join([dir, subdir, "ebin"])
|> Code.append_path
end
end
end

View File

@ -35,8 +35,8 @@ defmodule RabbitMQCtl.MixfileBase do
# Type "mix help compile.app" for more information
def application do
[applications: [:logger, :mix],
env: [scopes: [{:'rabbitmq-plugins', "Plugins"},
{:rabbitmqctl, "Ctl"}]]
env: [scopes: ['rabbitmq-plugins': :plugins,
rabbitmqctl: :ctl]]
]
end