forked from vernemq/vernemq
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvmq_diversity_cli.erl
More file actions
150 lines (135 loc) · 4.51 KB
/
vmq_diversity_cli.erl
File metadata and controls
150 lines (135 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
%% Copyright 2018 Erlio GmbH Basel Switzerland (http://erl.io)
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
-module(vmq_diversity_cli).
-export([register_cli/0]).
-behaviour(clique_handler).
register_cli() ->
register_config(),
register_cli_usage(),
show_cmd(),
load_cmd(),
reload_cmd(),
unload_cmd().
register_config() ->
ConfigKeys =
["vmq_diversity.keep_state"],
[
clique:register_config([Key], fun register_config_callback/2)
|| Key <- ConfigKeys
],
ok = clique:register_config_whitelist(ConfigKeys).
register_config_callback(_, _) ->
ok.
register_cli_usage() ->
clique:register_usage(["vmq-admin", "script"], script_usage()),
clique:register_usage(["vmq-admin", "script", "load"], load_usage()),
clique:register_usage(["vmq-admin", "script", "reload"], reload_usage()),
clique:register_usage(["vmq-admin", "script", "unload"], unload_usage()),
clique:register_usage(["vmq-admin", "script", "show"], show_usage()).
show_cmd() ->
Cmd = ["vmq-admin", "script", "show"],
Callback = fun(_, _, _) ->
AllStats = vmq_diversity_script_sup:stats(),
lists:foldl(
fun({Script, Stats}, Acc) ->
[
clique_status:table([[{script, Script} | Stats]])
| Acc
]
end,
[],
AllStats
)
end,
clique:register_command(Cmd, [], [], Callback).
load_cmd() ->
Cmd = ["vmq-admin", "script", "load"],
KeySpecs = [path_keyspec()],
Callback = fun(_, [{path, Path}], _) ->
case vmq_diversity:load_script(Path) of
{error, _} ->
[clique_status:text("Can't load script!")];
{ok, _} ->
[clique_status:text("Script successfully loaded!")]
end
end,
clique:register_command(Cmd, KeySpecs, [], Callback).
reload_cmd() ->
Cmd = ["vmq-admin", "script", "reload"],
KeySpecs = [path_keyspec()],
Callback = fun(_, [{path, Path}], _) ->
case vmq_diversity:reload_script(Path) of
{error, _} ->
[clique_status:text("Can't reload script!")];
ok ->
[clique_status:text("Script successfully reloaded!")]
end
end,
clique:register_command(Cmd, KeySpecs, [], Callback).
unload_cmd() ->
Cmd = ["vmq-admin", "script", "unload"],
KeySpecs = [path_keyspec()],
Callback = fun(_, [{path, Path}], _) ->
case vmq_diversity:unload_script(Path) of
{error, _} ->
[clique_status:text("Can't unload script!")];
ok ->
[clique_status:text("Script successfully unloaded!")]
end
end,
clique:register_command(Cmd, KeySpecs, [], Callback).
script_usage() ->
[
"vmq-admin script <sub-command>\n\n",
" Manage the plugin scripts on this VerneMQ node.\n\n",
" Sub-commands:\n",
" show Show the status of loaded scripts\n",
" load Load a script file\n",
" reload Reload a script file\n",
" unload Unload a script file\n\n",
" Use --help after a sub-command for more details.\n"
].
load_usage() ->
[
"vmq-admin script load path=<PathToScript>\n\n",
" Loads the script at <PathToScript> into VerneMQ.",
"\n\n"
].
reload_usage() ->
[
"vmq-admin script reload path=<PathToScript>\n\n",
" Reloads the script at <PathToScript> into VerneMQ.",
"\n\n"
].
unload_usage() ->
[
"vmq-admin script unload path=<PathToScript>\n\n",
" Unloads the script at <PathToScript>.",
"\n\n"
].
show_usage() ->
[
"vmq-admin script show\n\n",
" Shows the information of the loaded scripts.",
"\n\n"
].
path_keyspec() ->
{path, [
{typecast, fun(Path) ->
case filelib:is_file(Path) of
true -> Path;
false -> {error, {invalid_value, Path}}
end
end}
]}.