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

Commit

Permalink
Allow loading definitions from file or directory
Browse files Browse the repository at this point in the history
Fixes #665

(cherry picked from commit 2ca24e9)
  • Loading branch information
lukebakken authored and michaelklishin committed Mar 12, 2019
1 parent c9bd17f commit eb83cba
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
4 changes: 2 additions & 2 deletions priv/schema/rabbitmq_management.schema
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
%% See http://www.rabbitmq.com/management.html for details
%% ----------------------------------------------------------------------------

% {rabbitmq_management,
% [%% Pre-Load schema definitions from the following JSON file. See
%% Load definitions from a JSON file or directory of files. See
%% http://www.rabbitmq.com/management.html#load-definitions
%%
%% {load_definitions, "/path/to/schema.json"},
%% {load_definitions, "/path/to/schemas"},
{mapping, "management.load_definitions", "rabbitmq_management.load_definitions",
[{datatype, string},
{validators, ["file_accessible"]}]}.
Expand Down
41 changes: 32 additions & 9 deletions src/rabbit_mgmt_load_definitions.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,38 @@
{enables, empty_db_check}]}).

maybe_load_definitions() ->
{ok, File} = application:get_env(rabbitmq_management, load_definitions),
case File of
none -> ok;
_ -> case file:read_file(File) of
{ok, Body} -> rabbit_log:info(
"Applying definitions from: ~s", [File]),
load_definitions(Body);
{error, E} -> {error, {could_not_read_defs, {File, E}}}
end
case application:get_env(rabbitmq_management, load_definitions) of
{ok, none} ->
ok;
{ok, FileOrDir} ->
IsDir = filelib:is_dir(FileOrDir),
maybe_load_definitions_from(IsDir, FileOrDir)
end.

maybe_load_definitions_from(true, Dir) ->
rabbit_log:info("Applying definitions from directory: ~s", [Dir]),
load_definitions_from_files(file:list_dir(Dir));
maybe_load_definitions_from(false, File) ->
load_definitions_from_file(File).

load_definitions_from_files({ok, Filenames}) ->
load_definitions_from_filenames(lists:sort(Filenames));
load_definitions_from_files({error, E}) ->
{error, {could_not_read_defs, E}}.

load_definitions_from_filenames([]) ->
ok;
load_definitions_from_filenames([File|Rest]) ->
load_definitions_from_file(File),
load_definitions_from_filenames(Rest).

load_definitions_from_file(File) ->
case file:read_file(File) of
{ok, Body} ->
rabbit_log:info("Applying definitions from: ~s", [File]),
load_definitions(Body);
{error, E} ->
{error, {could_not_read_defs, {File, E}}}
end.

load_definitions(Body) ->
Expand Down

0 comments on commit eb83cba

Please sign in to comment.