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

Commit e087171

Browse files
author
Daniil Fedotov
committed
Aliases in behaviour. List plugins command
1 parent 63fc022 commit e087171

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+341
-32
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ all:
55
tests: all
66
mix test
77
plugins: all
8+
rm rabbitmq-plugins
89
ln -s rabbitmqctl rabbitmq-plugins
910

lib/rabbit_common/records.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ defmodule RabbitCommon.Records do
1818
import Record, only: [defrecord: 2, extract: 2]
1919

2020
defrecord :amqqueue, extract(:amqqueue, from_lib: "rabbit_common/include/rabbit.hrl")
21+
defrecord :plugin, extract(:plugin, from_lib: "rabbit_common/include/rabbit.hrl")
2122
end

lib/rabbitmq/cli/command_behaviour.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ defmodule RabbitMQ.CLI.CommandBehaviour do
2222
@callback banner(List.t, Map.t) :: String.t
2323
@callback run(List.t, Map.t) :: any
2424
@callback switches() :: Keyword.t
25+
@callback aliases() :: Keyword.t
2526
end

lib/rabbitmq/cli/ctl/command_modules.ex

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616

1717
defmodule RabbitMQ.CLI.Ctl.CommandModules do
18+
@commands_ns ~r/RabbitMQ.CLI.(.*).Commands/
19+
1820
def module_map do
1921
case Application.get_env(:rabbitmqctl, :commands) do
2022
nil -> load;
@@ -41,28 +43,32 @@ defmodule RabbitMQ.CLI.Ctl.CommandModules do
4143
end
4244

4345
defp load_commands(scope) do
44-
modules = loadable_modules()
45-
modules
46-
|> Enum.filter(fn(path) ->
47-
to_string(path) =~ ~r/RabbitMQ.CLI.*.Commands/
46+
ctl_and_plugin_modules
47+
|> Enum.filter(fn(mod) ->
48+
to_string(mod) =~ @commands_ns
49+
and
50+
implements_command_behaviour?(mod)
51+
and
52+
command_in_scope(mod, scope)
4853
end)
49-
|> Enum.map(fn(path) ->
50-
Path.rootname(path, '.beam')
51-
|> String.to_atom
52-
|> Code.ensure_loaded
53-
end)
54-
|> Enum.filter_map(fn({res, _}) -> res == :module end,
55-
fn({_, mod}) -> command_tuple(mod) end)
56-
|> Enum.filter(fn({_, cmd}) -> command_in_scope(cmd, scope) end)
54+
|> Enum.map(&command_tuple/1)
5755
|> Map.new
5856
end
5957

60-
defp loadable_modules do
61-
:code.get_path()
62-
|> Enum.flat_map(fn(path) ->
63-
{:ok, modules} = :erl_prim_loader.list_dir(path)
64-
modules
65-
end)
58+
defp ctl_and_plugin_modules do
59+
# No plugins so far
60+
applications = [:rabbitmqctl]
61+
applications
62+
|> Enum.flat_map(fn(app) -> Application.spec(app, :modules) end)
63+
end
64+
65+
66+
defp implements_command_behaviour?(nil) do
67+
false
68+
end
69+
defp implements_command_behaviour?(module) do
70+
Enum.member?(module.module_info(:attributes)[:behaviour] || [],
71+
RabbitMQ.CLI.CommandBehaviour)
6672
end
6773

6874
defp command_tuple(cmd) do
@@ -107,8 +113,20 @@ defmodule RabbitMQ.CLI.Ctl.CommandModules do
107113
false
108114
end
109115
defp command_in_scope(cmd, scope) do
110-
cmd
111-
|> to_string
112-
|> String.contains?("RabbitMQ.CLI.#{scope}.Commands")
116+
Enum.member?(command_scopes(cmd), scope)
117+
end
118+
119+
defp command_scopes(cmd) do
120+
case :erlang.function_exported(cmd, :scopes, 0) do
121+
true ->
122+
cmd.scopes()
123+
false ->
124+
@commands_ns
125+
|> Regex.run(to_string(cmd), capture: :all_but_first)
126+
|> List.first
127+
|> to_snake_case
128+
|> String.to_atom
129+
|> List.wrap
130+
end
113131
end
114132
end

lib/rabbitmq/cli/ctl/commands/add_user_command.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.AddUserCommand do
2323
def merge_defaults(args, opts), do: {args, opts}
2424

2525
def switches(), do: []
26+
def aliases(), do: []
2627

2728
def validate(args, _) when length(args) < 2 do
2829
{:validation_failure, :not_enough_args}

lib/rabbitmq/cli/ctl/commands/add_vhost_command.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.AddVhostCommand do
2525
def merge_defaults(args, opts), do: {args, opts}
2626

2727
def switches(), do: []
28+
def aliases(), do: []
2829
def run([vhost], %{node: node_name}) do
2930
:rabbit_misc.rpc_call(node_name, :rabbit_vhost, :add, [vhost])
3031
end

lib/rabbitmq/cli/ctl/commands/authenticate_user_command.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.AuthenticateUserCommand do
2323
def validate([_,_], _), do: :ok
2424
def merge_defaults(args, opts), do: {args, opts}
2525
def switches(), do: []
26+
def aliases(), do: []
2627

2728
def run([user, password], %{node: node_name}) do
2829
:rabbit_misc.rpc_call(node_name,

lib/rabbitmq/cli/ctl/commands/cancel_sync_queue_command.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.CancelSyncQueueCommand do
2323
def flags, do: [:vhost]
2424

2525
def switches, do: []
26+
def aliases, do: []
2627

2728
def usage, do: "cancel_sync_queue [-p <vhost>] queue"
2829

lib/rabbitmq/cli/ctl/commands/change_password_command.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ChangePasswordCommand do
2121
def merge_defaults(args, opts), do: {args, opts}
2222

2323
def switches(), do: []
24+
def aliases(), do: []
2425

2526
def validate(args, _) when length(args) < 2, do: {:validation_failure, :not_enough_args}
2627
def validate([_|_] = args, _) when length(args) > 2, do: {:validation_failure, :too_many_args}

lib/rabbitmq/cli/ctl/commands/clear_parameter_command.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ClearParameterCommand do
1919
@flags [:vhost]
2020

2121
def switches(), do: []
22+
def aliases(), do: []
2223
def merge_defaults(args, opts) do
2324
default_opts = Map.merge(opts, %{vhost: "/"})
2425
{args, default_opts}

0 commit comments

Comments
 (0)