Skip to content

Commit 75f74f3

Browse files
committed
Read input file support
[skip ci]
1 parent 42786aa commit 75f74f3

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

deps/rabbit/src/rabbit_cli_commands.erl

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
-include_lib("rabbit_common/include/resource.hrl").
77

88
-export([argparse_def/0, run_command/1, do_run_command/1]).
9-
-export([cmd_list_exchanges/1]).
9+
-export([cmd_list_exchanges/1,
10+
cmd_import_definitions/1]).
1011

1112
-rabbitmq_command(
1213
{#{cli => ["declare", "exchange"],
@@ -31,6 +32,12 @@
3132
#{help => "List exchanges",
3233
handler => {?MODULE, cmd_list_exchanges}}]}).
3334

35+
-rabbitmq_command(
36+
{#{cli => ["import", "definitions"]},
37+
[argparse_def_file_input,
38+
#{help => "Import definitions",
39+
handler => {?MODULE, cmd_import_definitions}}]}).
40+
3441
argparse_def() ->
3542
#{argparse_def := ArgparseDef} = get_discovered_commands(),
3643
ArgparseDef.
@@ -94,6 +101,9 @@ expand_argparse_def(Defs) when is_list(Defs) ->
94101
(argparse_def_record_stream, Acc) ->
95102
Def = rabbit_cli_io:argparse_def(record_stream),
96103
rabbit_cli:merge_argparse_def(Acc, Def);
104+
(argparse_def_file_input, Acc) ->
105+
Def = rabbit_cli_io:argparse_def(file_input),
106+
rabbit_cli:merge_argparse_def(Acc, Def);
97107
(Def, Acc) ->
98108
Def1 = expand_argparse_def(Def),
99109
rabbit_cli:merge_argparse_def(Acc, Def1)
@@ -167,3 +177,12 @@ cmd_list_exchanges(#{arg_map := ArgMap, io := IO}) ->
167177
{error, _} = Error ->
168178
Error
169179
end.
180+
181+
cmd_import_definitions(#{arg_map := ArgMap, io := IO}) ->
182+
case rabbit_cli_io:read_file(IO, ArgMap) of
183+
{ok, Data} ->
184+
rabbit_cli_io:format(IO, "Import definitions:~n ~s~n", [Data]),
185+
ok;
186+
{error, _} = Error ->
187+
Error
188+
end.

deps/rabbit/src/rabbit_cli_io.erl

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
stop/1,
99
argparse_def/1,
1010
display_help/1,
11+
format/3,
1112
start_record_stream/4,
1213
push_new_record/3,
13-
end_record_stream/2]).
14+
end_record_stream/2,
15+
read_file/2]).
1416
-export([init/1,
1517
handle_call/3,
1618
handle_cast/2,
@@ -48,13 +50,29 @@ argparse_def(record_stream) ->
4850
default => plain,
4951
help => "Format output acccording to <FORMAT>"}
5052
]
53+
};
54+
argparse_def(file_input) ->
55+
#{arguments =>
56+
[
57+
#{name => input,
58+
long => "-input",
59+
short => $i,
60+
type => string,
61+
nargs => 1,
62+
help => "Read input from file <FILE>"}
63+
]
5164
}.
5265

5366
display_help(#{io := {transport, Transport}} = Context) ->
5467
Transport ! {io_cast, {?FUNCTION_NAME, Context}};
5568
display_help(#{io := IO} = Context) ->
5669
gen_server:cast(IO, {?FUNCTION_NAME, Context}).
5770

71+
format({transport, Transport}, Format, Args) ->
72+
Transport ! {io_cast, {?FUNCTION_NAME, Format, Args}};
73+
format(IO, Format, Args) ->
74+
gen_server:cast(IO, {?FUNCTION_NAME, Format, Args}).
75+
5876
start_record_stream({transport, Transport}, Name, Fields, ArgMap) ->
5977
Transport ! {io_call, self(), {?FUNCTION_NAME, Name, Fields, ArgMap}},
6078
receive Ret -> Ret end;
@@ -74,6 +92,14 @@ end_record_stream({transport, Transport}, #{name := Name}) ->
7492
end_record_stream(IO, #{name := Name}) ->
7593
gen_server:cast(IO, {?FUNCTION_NAME, Name}).
7694

95+
read_file({transport, Transport}, ArgMap) ->
96+
Transport ! {io_call, self(), {?FUNCTION_NAME, ArgMap}},
97+
receive Ret -> Ret end;
98+
read_file(IO, ArgMap)
99+
when is_pid(IO) andalso
100+
is_map(ArgMap) ->
101+
gen_server:call(IO, {?FUNCTION_NAME, ArgMap}).
102+
77103
init(#{progname := Progname}) ->
78104
process_flag(trap_exit, true),
79105
State = #?MODULE{progname = Progname},
@@ -91,6 +117,9 @@ handle_call(
91117
{ok, State2} = format_record_stream_start(Name, State1),
92118

93119
{noreply, State2};
120+
handle_call({read_file, ArgMap}, From, State) ->
121+
{ok, State1} = do_read_file(ArgMap, From, State),
122+
{noreply, State1};
94123
handle_call(stop, _From, State) ->
95124
{stop, normal, ok, State};
96125
handle_call(_Request, _From, State) ->
@@ -106,6 +135,9 @@ handle_cast(
106135
Help = argparse:help(ArgparseDef, Options),
107136
io:format("~s~n", [Help]),
108137
{noreply, State};
138+
handle_cast({format, Format, Args}, State) ->
139+
io:format(Format, Args),
140+
{noreply, State};
109141
handle_cast({push_new_record, Name, Record}, State) ->
110142
{ok, State1} = format_record(Name, Record, State),
111143
{noreply, State1};
@@ -249,3 +281,23 @@ isatty(IoDevice) ->
249281
_ ->
250282
false
251283
end.
284+
285+
do_read_file(#{input := "-"}, From, State) ->
286+
Ret = read_stdin(<<>>),
287+
gen:reply(From, Ret),
288+
{ok, State};
289+
do_read_file(#{input := Filename}, From, State) ->
290+
Ret = file:read_file(Filename),
291+
gen:reply(From, Ret),
292+
{ok, State}.
293+
294+
read_stdin(Buf) ->
295+
case file:read(standard_io, 4096) of
296+
{ok, Data} ->
297+
Buf1 = [Buf, Data],
298+
read_stdin(Buf1);
299+
eof ->
300+
{ok, Buf};
301+
{error, _} = Error ->
302+
Error
303+
end.

0 commit comments

Comments
 (0)