Comparison

plugins/mod_admin_shell.lua @ 13734:c133635d0bc6 13.0

mod_admin_shell: Improved error handling for shell-invoked commands
author Matthew Wild <mwild1@gmail.com>
date Mon, 17 Feb 2025 18:25:52 +0000
parent 13732:1465b1e305df
child 13735:5e8a707f1dbf
comparison
equal deleted inserted replaced
13733:48c056c10e5a 13734:c133635d0bc6
263 263
264 local section_name, command = args[1], args[2]; 264 local section_name, command = args[1], args[2];
265 265
266 local section_mt = getmetatable(def_env[section_name]); 266 local section_mt = getmetatable(def_env[section_name]);
267 local section_help = section_mt and section_mt.help; 267 local section_help = section_mt and section_mt.help;
268 local command_help = section_help.commands[command]; 268 local command_help = section_help and section_help.commands[command];
269
270 if not command_help then
271 return nil, "Command not found. Is the necessary module loaded?";
272 end
269 273
270 local fmt = { "%s"; ":%s("; ")" }; 274 local fmt = { "%s"; ":%s("; ")" };
271 275
272 local flags;
273 if command_help.flags then 276 if command_help.flags then
274 flags = parse_args(args, command_help.flags); 277 local flags, flags_err, flags_err_extra = parse_args(args, command_help.flags);
278 if not flags then
279 if flags_err == "missing-value" then
280 return nil, "Expected value after "..flags_err_extra;
281 elseif flags_err == "param-not-found" then
282 return nil, "Unknown parameter: "..flags_err_extra;
283 end
284 return nil, flags_err;
285 end
275 286
276 table.remove(flags, 2); 287 table.remove(flags, 2);
277 table.remove(flags, 1); 288 table.remove(flags, 1);
278 289
279 local n_fixed_args = #command_help.args; 290 local n_fixed_args = #command_help.args;
378 local source; 389 local source;
379 if line:match("^{") then 390 if line:match("^{") then
380 -- Input is a serialized array of strings, typically from 391 -- Input is a serialized array of strings, typically from
381 -- a command-line invocation of 'prosodyctl shell something' 392 -- a command-line invocation of 'prosodyctl shell something'
382 source, flags = process_cmd_line(line); 393 source, flags = process_cmd_line(line);
394 if not source then
395 send_result(false, flags);
396 return;
397 end
383 end 398 end
384 399
385 local chunkname = "=console"; 400 local chunkname = "=console";
386 -- luacheck: ignore 311/err 401 -- luacheck: ignore 311/err
387 local chunk, err = envload(source or ("return "..line), chunkname, env); 402 local chunk, err = envload(source or ("return "..line), chunkname, env);
2711 commands = {}; 2726 commands = {};
2712 }; 2727 };
2713 section_mt.help = section_help; 2728 section_mt.help = section_help;
2714 end 2729 end
2715 2730
2716 if command.flags and command.flags.stop_on_positional == nil then 2731 if command.flags then
2717 command.flags.stop_on_positional = false; 2732 if command.flags.stop_on_positional == nil then
2733 command.flags.stop_on_positional = false;
2734 end
2735 if command.flags.strict == nil then
2736 command.flags.strict = true;
2737 end
2718 end 2738 end
2719 2739
2720 section_help.commands[command.name] = { 2740 section_help.commands[command.name] = {
2721 desc = command.desc; 2741 desc = command.desc;
2722 full = command.help; 2742 full = command.help;