Skip to content

Commit 46f7e2d

Browse files
committed
wip
1 parent 9f3a977 commit 46f7e2d

File tree

4 files changed

+269
-72
lines changed

4 files changed

+269
-72
lines changed

deps/rabbit/Makefile

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,20 +152,24 @@ DEP_PLUGINS = rabbit_common/mk/rabbitmq-plugin.mk
152152
include ../../rabbitmq-components.mk
153153
include ../../erlang.mk
154154

155-
CLI_SCRIPTS := scripts/rmq
155+
ESCRIPT_NAME := rabbit_cli
156+
ESCRIPT_FILE := scripts/rmq
156157

157-
ebin/$(PROJECT).app:: $(CLI_SCRIPTS)
158+
ebin/$(PROJECT).app:: $(ESCRIPT_FILE)
158159

159-
$(CLI_SCRIPTS): ebin/rabbit_cli.beam
160-
$(gen_verbose) echo '#!/usr/bin/env escript' > "$@"
161-
$(verbose) echo '%%! +sbtu +A1 -start_epmd false' >> "$@"
162-
$(verbose) cat $^ >> "$@"
163-
$(verbose) chmod a+x "$@"
160+
$(ESCRIPT_FILE): ebin/rabbit_cli.beam ebin/rabbit_cli_io.beam
161+
$(gen_verbose) cd .. && $(ESCRIPT_ZIP) $(abspath $(ESCRIPT_ZIP_FILE)) $(patsubst %,$(PROJECT)/%,$^)
162+
$(gen_verbose) printf "%s\n" \
163+
"#!$(ESCRIPT_SHEBANG)" \
164+
"%% $(ESCRIPT_COMMENT)" \
165+
"%%! $(ESCRIPT_EMU_ARGS)" > $(ESCRIPT_FILE)
166+
$(verbose) cat $(abspath $(ESCRIPT_ZIP_FILE)) >> $(ESCRIPT_FILE)
167+
$(verbose) chmod a+x $(ESCRIPT_FILE)
164168

165169
clean:: clean-cli
166170

167171
clean-cli:
168-
$(gen_verbose) rm -f $(CLI_SCRIPTS)
172+
$(gen_verbose) rm -f $(ESCRIPT_FILE)
169173

170174
ifeq ($(strip $(BATS)),)
171175
BATS := $(ERLANG_MK_TMP)/bats/bin/bats

deps/rabbit/src/rabbit_cli.erl

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,27 @@
55
-export([main/1]).
66

77
main(Args) ->
8-
case run_ci(Args) of
8+
case run_cli(Args) of
99
ok ->
1010
erlang:halt();
1111
{error, ErrorMsg} ->
1212
io:format(standard_error, "Error: ~ts~n", [ErrorMsg]),
1313
erlang:halt(1)
1414
end.
1515

16-
run_ci(Args) ->
16+
run_cli(Args) ->
1717
maybe
18-
io:format("Args = ~p~n", [Args]),
1918
{ok, RemainingArgs, Nodename} ?= lookup_rabbitmq_nodename(Args),
20-
io:format("Args = ~p~n", [RemainingArgs]),
21-
io:format("Nodename = ~p~n", [Nodename]),
2219
{ok, _} ?= net_kernel:start(undefined, #{name_domain => shortnames}),
2320
true ?= net_kernel:connect_node(Nodename),
24-
io:format("This node = ~p~n", [node()]),
25-
%{ok, CommandMap} ?= lookup_command_map(Nodename),
26-
%io:format("CommandMap= ~p~n", [CommandMap]),
27-
Ret ?= run_command(Nodename, RemainingArgs),
28-
io:format("Ret = ~p~n", [Ret]),
21+
22+
{ok, IO} ?= rabbit_cli_io:start_link(),
23+
try
24+
Ret = run_command(Nodename, RemainingArgs, IO),
25+
io:format("Ret = ~p~n", [Ret])
26+
after
27+
rabbit_cli_io:stop(IO)
28+
end,
2929
ok
3030
end.
3131

@@ -44,9 +44,20 @@ lookup_rabbitmq_nodename(Args) ->
4444

4545
get_rabbitmq_nodename_from_args(Args) ->
4646
Definition = #{arguments =>
47-
[#{name => node,
47+
[#{name => verbose,
48+
long => "-verbose",
49+
short => $v,
50+
action => count,
51+
help =>
52+
"Be verbose; can be specified multiple times to "
53+
"increase "
54+
"verbosity"},
55+
#{name => node,
4856
long => "-node",
49-
short => $n},
57+
short => $n,
58+
type => string,
59+
nargs => 1,
60+
help => "Name of the node to control"},
5061
#{name => rest,
5162
nargs => list,
5263
action => extend,
@@ -138,9 +149,9 @@ complete_nodename(Nodename) ->
138149
% io:format("Script = ~p~n", [ScriptDir]),
139150
% ok.
140151

141-
run_command(Nodename, Args) ->
152+
run_command(Nodename, Args, IO) ->
142153
try
143-
erpc:call(Nodename, rabbit_cli_commands, run_command, [Args])
154+
erpc:call(Nodename, rabbit_cli_commands, run_command, [Args, IO])
144155
catch
145156
error:{erpc, Reason} ->
146157
{error, Reason}

deps/rabbit/src/rabbit_cli_commands.erl

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,88 @@
22

33
-include_lib("kernel/include/logger.hrl").
44

5-
-export([commands/0, run_command/1]).
6-
-export([list_queues/1]).
5+
-export([commands/0, run_command/2]).
6+
-export([list_exchanges/2]).
77

88
commands() ->
9+
%% Extract the commands from module attributes like feature flags and boot
10+
%% steps.
911
#{
1012
arguments =>
1113
[
12-
#{name => verbose,
13-
short => $v,
14-
action => count,
15-
help =>
16-
"Be verbose; can be specified multiple times to increase "
17-
"verbosity"}
14+
#{name => rest,
15+
nargs => list,
16+
action => extend,
17+
required => false,
18+
default => []}
1819
],
1920
commands =>
2021
#{
2122
"list" =>
22-
#{commands =>
23-
#{"queues" =>
24-
#{handler => {?MODULE, list_queues}}
23+
#{
24+
commands =>
25+
#{"exchanges" =>
26+
#{
27+
handler => {?MODULE, list_exchanges}
28+
}
2529
}
2630
}
2731
}
2832
}.
2933

30-
run_command(Args) ->
34+
run_command(Args, IO) ->
3135
Definition = commands(),
3236
case argparse:parse(Args, Definition, #{}) of
33-
{ok, ArgMap, CmdPath, #{handler := {Mod, Fun}} = Command} ->
34-
?LOG_ALERT("ArgMap=~0p~nCmdPath=~0p~nCommand=~0p", [ArgMap, CmdPath, Command]),
35-
Mod:Fun(ArgMap),
36-
ok;
37-
{error, Reason} ->
38-
?LOG_ALERT("Reason=~0p", [Reason]),
39-
ok
37+
{ok, ArgMap, _CmdPath, Command} ->
38+
%% TODO: Put both processes under the rabbit supervision tree.
39+
RunnerPid = command_runner(Command, ArgMap, IO),
40+
RunnerMRef = erlang:monitor(process, RunnerPid),
41+
receive
42+
{'DOWN', RunnerMRef, _, _, Reason} ->
43+
{ok, Reason}
44+
end;
45+
{error, _Reason} = Error ->
46+
Error
4047
end.
4148

42-
list_queues(ArgMap) ->
43-
?LOG_ALERT("Listing queues, ~0p", [ArgMap]),
44-
ok.
49+
command_runner(#{handler := {Mod, Fun}} = _Command, ArgMap, IO) ->
50+
spawn_link(Mod, Fun, [ArgMap, IO]).
51+
52+
list_exchanges(ArgMap, IO) ->
53+
InfoKeys = rabbit_exchange:info_keys(),
54+
Fields = lists:map(
55+
fun
56+
(name = Key) ->
57+
#{name => Key, type => resource};
58+
(type = Key) ->
59+
#{name => Key, type => string};
60+
(durable = Key) ->
61+
#{name => Key, type => boolean};
62+
(auto_delete = Key) ->
63+
#{name => Key, type => boolean};
64+
(internal = Key) ->
65+
#{name => Key, type => boolean};
66+
(arguments = Key) ->
67+
#{name => Key, type => term};
68+
(policy = Key) ->
69+
#{name => Key, type => string};
70+
(user_who_performed_action = Key) ->
71+
#{name => Key, type => string};
72+
(Key) ->
73+
#{name => Key, type => term}
74+
end, InfoKeys),
75+
IOArgs = maps:get(rest, ArgMap, []),
76+
case rabbit_cli_io:start_record_stream(IO, exchanges, Fields, IOArgs) of
77+
{ok, Stream} ->
78+
Exchanges = rabbit_exchange:list(),
79+
lists:foreach(
80+
fun(Exchange) ->
81+
Record0 = rabbit_exchange:info(Exchange),
82+
Record1 = lists:sublist(Record0, length(Fields)),
83+
Record2 = [Value || {_Key, Value} <- Record1],
84+
rabbit_cli_io:push_new_record(IO, Stream, Record2)
85+
end, Exchanges),
86+
rabbit_cli_io:end_record_stream(IO, Stream);
87+
{error, _} = Error ->
88+
Error
89+
end.

0 commit comments

Comments
 (0)