From 159f8f7bc1509d1bacc07ddb06dd0dc23c4421c0 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 22 Mar 2018 16:23:06 +0000 Subject: prosodyctl: Run commands inside async context --- prosodyctl | 127 ++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 70 insertions(+), 57 deletions(-) (limited to 'prosodyctl') diff --git a/prosodyctl b/prosodyctl index ebbba0ed..07b68e69 100755 --- a/prosodyctl +++ b/prosodyctl @@ -1232,77 +1232,90 @@ end --------------------- -if command and command:match("^mod_") then -- Is a command in a module - local module_name = command:match("^mod_(.+)"); - local ret, err = modulemanager.load("*", module_name); - if not ret then - show_message("Failed to load module '"..module_name.."': "..err); - os.exit(1); - end +local async = require "util.async"; +local watchers = { + error = function (_, err) + error(err); + end; + waiting = function () + server.loop(); + end; +}; +local command_runner = async.runner(function () + if command and command:match("^mod_") then -- Is a command in a module + local module_name = command:match("^mod_(.+)"); + local ret, err = modulemanager.load("*", module_name); + if not ret then + show_message("Failed to load module '"..module_name.."': "..err); + os.exit(1); + end - table.remove(arg, 1); + table.remove(arg, 1); - local module = modulemanager.get_module("*", module_name); - if not module then - show_message("Failed to load module '"..module_name.."': Unknown error"); - os.exit(1); - end + local module = modulemanager.get_module("*", module_name); + if not module then + show_message("Failed to load module '"..module_name.."': Unknown error"); + os.exit(1); + end - if not modulemanager.module_has_method(module, "command") then - show_message("Fail: mod_"..module_name.." does not support any commands"); - os.exit(1); - end + if not modulemanager.module_has_method(module, "command") then + show_message("Fail: mod_"..module_name.." does not support any commands"); + os.exit(1); + end - local ok, ret = modulemanager.call_module_method(module, "command", arg); - if ok then - if type(ret) == "number" then - os.exit(ret); - elseif type(ret) == "string" then - show_message(ret); + local ok, ret = modulemanager.call_module_method(module, "command", arg); + if ok then + if type(ret) == "number" then + os.exit(ret); + elseif type(ret) == "string" then + show_message(ret); + end + os.exit(0); -- :) + else + show_message("Failed to execute command: "..error_messages[ret]); + os.exit(1); -- :( end - os.exit(0); -- :) - else - show_message("Failed to execute command: "..error_messages[ret]); - os.exit(1); -- :( end -end -if not commands[command] then -- Show help for all commands - function show_usage(usage, desc) - print(" "..usage); - print(" "..desc); - end + if not commands[command] then -- Show help for all commands + function show_usage(usage, desc) + print(" "..usage); + print(" "..desc); + end - print("prosodyctl - Manage a Prosody server"); - print(""); - print("Usage: "..arg[0].." COMMAND [OPTIONS]"); - print(""); - print("Where COMMAND may be one of:\n"); + print("prosodyctl - Manage a Prosody server"); + print(""); + print("Usage: "..arg[0].." COMMAND [OPTIONS]"); + print(""); + print("Where COMMAND may be one of:\n"); - local hidden_commands = require "util.set".new{ "register", "unregister", "addplugin" }; - local commands_order = { "adduser", "passwd", "deluser", "start", "stop", "restart", "reload", "about" }; + local hidden_commands = require "util.set".new{ "register", "unregister", "addplugin" }; + local commands_order = { "adduser", "passwd", "deluser", "start", "stop", "restart", "reload", "about" }; - local done = {}; + local done = {}; - for _, command_name in ipairs(commands_order) do - local command = commands[command_name]; - if command then - command{ "--help" }; - print"" - done[command_name] = true; + for _, command_name in ipairs(commands_order) do + local command = commands[command_name]; + if command then + command{ "--help" }; + print"" + done[command_name] = true; + end end - end - for command_name, command in pairs(commands) do - if not done[command_name] and not hidden_commands:contains(command_name) then - command{ "--help" }; - print"" - done[command_name] = true; + for command_name, command in pairs(commands) do + if not done[command_name] and not hidden_commands:contains(command_name) then + command{ "--help" }; + print"" + done[command_name] = true; + end end - end - os.exit(0); -end + os.exit(0); + end + + os.exit(commands[command]({ select(2, unpack(arg)) })); +end, watchers); -os.exit(commands[command]({ select(2, unpack(arg)) })); +command_runner:run(true); -- cgit v1.2.3