From b9c4095511d8048910fbcb67865ec6eb86283bc6 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Fri, 30 Aug 2019 16:42:28 -0300 Subject: cmd: catch errors loading command modules This should be useful for external modules. --- src/luarocks/cmd.lua | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/luarocks/cmd.lua b/src/luarocks/cmd.lua index 328afe3f..eba5e9a8 100644 --- a/src/luarocks/cmd.lua +++ b/src/luarocks/cmd.lua @@ -460,19 +460,30 @@ function cmd.run_command(description, commands, external_namespace, ...) local cmd_modules = {} for name, module in pairs(commands) do - cmd_modules[name] = require(module) - if not cmd_modules[name].add_to_parser then - cmd_modules[name].add_to_parser = function(parser) - parser:command(name, cmd_modules[name].help, util.see_also()) - :summary(cmd_modules[name].help_summary) - :handle_options(false) - :argument("input") - :args("*") + local pok, mod = pcall(require, module) + if pok and type(mod) == "table" then + if not mod.add_to_parser then + mod.add_to_parser = function(parser) + parser:command(name, mod.help, util.see_also()) + :summary(mod.help_summary) + :handle_options(false) + :argument("input") + :args("*") + end + local original_command = mod.command + if original_command then + mod.command = function(args) + return original_command(args, unpack(args.input)) + end + end end - local original_command = cmd_modules[name].command - cmd_modules[name].command = function(args) - return original_command(args, unpack(args.input)) + if mod.command then + cmd_modules[name] = mod + else + util.warning("command module " .. module .. " does not implement command(), skipping") end + else + util.warning("failed to load command module " .. module) end end -- cgit v1.2.3-55-g6feb