Skip to content
This repository was archived by the owner on Nov 18, 2020. It is now read-only.

Commit

Permalink
Aliases in behaviour. List plugins command
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniil Fedotov committed Jun 23, 2016
1 parent 63fc022 commit e087171
Show file tree
Hide file tree
Showing 55 changed files with 341 additions and 32 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ all:
tests: all
mix test
plugins: all
rm rabbitmq-plugins
ln -s rabbitmqctl rabbitmq-plugins

1 change: 1 addition & 0 deletions lib/rabbit_common/records.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/command_behaviour.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
60 changes: 39 additions & 21 deletions lib/rabbitmq/cli/ctl/command_modules.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/add_user_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/add_vhost_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/authenticate_user_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/cancel_sync_queue_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/change_password_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/clear_parameter_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/clear_password_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/clear_permissions_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/close_connection_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/cluster_status_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/delete_user_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/delete_vhost_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/environment_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/force_reset_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/help_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/rabbitmq/cli/ctl/commands/join_cluster_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/list_bindings_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/list_channels_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
[]
Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/list_connections_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
[]
Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/list_consumers_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/list_exchanges_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/list_parameters_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/list_permissions_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
2 changes: 2 additions & 0 deletions lib/rabbitmq/cli/ctl/commands/list_queues_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/list_users_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/list_vhosts_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/node_health_check_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/purge_queue_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/report_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/reset_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/rotate_logs_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/set_cluster_name_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/set_parameter_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/ctl/commands/set_permissions_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit e087171

Please sign in to comment.