_site/cover/resp_command.COVER.html

1 %% Copyright (c) 2023 Peter Morgan <peter.james.morgan@gmail.com>
2 %%
3 %% Licensed under the Apache License, Version 2.0 (the "License");
4 %% you may not use this file except in compliance with the License.
5 %% You may obtain a copy of the License at
6 %%
7 %% http://www.apache.org/licenses/LICENSE-2.0
8 %%
9 %% Unless required by applicable law or agreed to in writing, software
10 %% distributed under the License is distributed on an "AS IS" BASIS,
11 %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 %% See the License for the specific language governing permissions and
13 %% limitations under the License.
14
15
16 -module(resp_command).
17
18
19 -export([info/1]).
20 -on_load(init/0).
21
22
23 info(Command) ->
24
:-(
case persistent_term:get(?MODULE) of
25 #{Command := Detail} ->
26
:-(
Detail;
27
28 _ ->
29
:-(
error(badarg, [Command])
30 end.
31
32
33 init() ->
34 1 persistent_term:put(
35 ?MODULE,
36 filelib:fold_files(
37 filename:join([resp:priv_dir(), "commands"]),
38 ".json$",
39 true,
40 fun
41 (Filename, A) ->
42 388 {ok, JSON} = file:read_file(Filename),
43 388 maps:merge(casefold(jsx:decode(JSON)), A)
44 end,
45 #{})).
46
47
48 casefold(M) ->
49 388 maps:fold(
50 fun
51 (Key, Value, A) ->
52 388 A#{string:casefold(Key) => Value}
53 end,
54 #{},
55 M).
Line Hits Source