Comparison

prosodyctl @ 8652:03bb534593cb

prosodyctl: Run commands inside async context
author Matthew Wild <mwild1@gmail.com>
date Thu, 22 Mar 2018 16:23:06 +0000
parent 8635:47e3b8b6f17a
child 8668:31c5abd49dfe
comparison
equal deleted inserted replaced
8651:1b7c5933b215 8652:03bb534593cb
1230 return ok and 0 or 2; 1230 return ok and 0 or 2;
1231 end 1231 end
1232 1232
1233 --------------------- 1233 ---------------------
1234 1234
1235 if command and command:match("^mod_") then -- Is a command in a module 1235 local async = require "util.async";
1236 local module_name = command:match("^mod_(.+)"); 1236 local watchers = {
1237 local ret, err = modulemanager.load("*", module_name); 1237 error = function (_, err)
1238 if not ret then 1238 error(err);
1239 show_message("Failed to load module '"..module_name.."': "..err); 1239 end;
1240 os.exit(1); 1240 waiting = function ()
1241 end 1241 server.loop();
1242 1242 end;
1243 table.remove(arg, 1); 1243 };
1244 1244 local command_runner = async.runner(function ()
1245 local module = modulemanager.get_module("*", module_name); 1245 if command and command:match("^mod_") then -- Is a command in a module
1246 if not module then 1246 local module_name = command:match("^mod_(.+)");
1247 show_message("Failed to load module '"..module_name.."': Unknown error"); 1247 local ret, err = modulemanager.load("*", module_name);
1248 os.exit(1); 1248 if not ret then
1249 end 1249 show_message("Failed to load module '"..module_name.."': "..err);
1250 1250 os.exit(1);
1251 if not modulemanager.module_has_method(module, "command") then 1251 end
1252 show_message("Fail: mod_"..module_name.." does not support any commands"); 1252
1253 os.exit(1); 1253 table.remove(arg, 1);
1254 end 1254
1255 1255 local module = modulemanager.get_module("*", module_name);
1256 local ok, ret = modulemanager.call_module_method(module, "command", arg); 1256 if not module then
1257 if ok then 1257 show_message("Failed to load module '"..module_name.."': Unknown error");
1258 if type(ret) == "number" then 1258 os.exit(1);
1259 os.exit(ret); 1259 end
1260 elseif type(ret) == "string" then 1260
1261 show_message(ret); 1261 if not modulemanager.module_has_method(module, "command") then
1262 end 1262 show_message("Fail: mod_"..module_name.." does not support any commands");
1263 os.exit(0); -- :) 1263 os.exit(1);
1264 else 1264 end
1265 show_message("Failed to execute command: "..error_messages[ret]); 1265
1266 os.exit(1); -- :( 1266 local ok, ret = modulemanager.call_module_method(module, "command", arg);
1267 end 1267 if ok then
1268 end 1268 if type(ret) == "number" then
1269 1269 os.exit(ret);
1270 if not commands[command] then -- Show help for all commands 1270 elseif type(ret) == "string" then
1271 function show_usage(usage, desc) 1271 show_message(ret);
1272 print(" "..usage); 1272 end
1273 print(" "..desc); 1273 os.exit(0); -- :)
1274 end 1274 else
1275 1275 show_message("Failed to execute command: "..error_messages[ret]);
1276 print("prosodyctl - Manage a Prosody server"); 1276 os.exit(1); -- :(
1277 print(""); 1277 end
1278 print("Usage: "..arg[0].." COMMAND [OPTIONS]"); 1278 end
1279 print(""); 1279
1280 print("Where COMMAND may be one of:\n"); 1280 if not commands[command] then -- Show help for all commands
1281 1281 function show_usage(usage, desc)
1282 local hidden_commands = require "util.set".new{ "register", "unregister", "addplugin" }; 1282 print(" "..usage);
1283 local commands_order = { "adduser", "passwd", "deluser", "start", "stop", "restart", "reload", "about" }; 1283 print(" "..desc);
1284 1284 end
1285 local done = {}; 1285
1286 1286 print("prosodyctl - Manage a Prosody server");
1287 for _, command_name in ipairs(commands_order) do 1287 print("");
1288 local command = commands[command_name]; 1288 print("Usage: "..arg[0].." COMMAND [OPTIONS]");
1289 if command then 1289 print("");
1290 command{ "--help" }; 1290 print("Where COMMAND may be one of:\n");
1291 print"" 1291
1292 done[command_name] = true; 1292 local hidden_commands = require "util.set".new{ "register", "unregister", "addplugin" };
1293 end 1293 local commands_order = { "adduser", "passwd", "deluser", "start", "stop", "restart", "reload", "about" };
1294 end 1294
1295 1295 local done = {};
1296 for command_name, command in pairs(commands) do 1296
1297 if not done[command_name] and not hidden_commands:contains(command_name) then 1297 for _, command_name in ipairs(commands_order) do
1298 command{ "--help" }; 1298 local command = commands[command_name];
1299 print"" 1299 if command then
1300 done[command_name] = true; 1300 command{ "--help" };
1301 end 1301 print""
1302 end 1302 done[command_name] = true;
1303 1303 end
1304 1304 end
1305 os.exit(0); 1305
1306 end 1306 for command_name, command in pairs(commands) do
1307 1307 if not done[command_name] and not hidden_commands:contains(command_name) then
1308 os.exit(commands[command]({ select(2, unpack(arg)) })); 1308 command{ "--help" };
1309 print""
1310 done[command_name] = true;
1311 end
1312 end
1313
1314
1315 os.exit(0);
1316 end
1317
1318 os.exit(commands[command]({ select(2, unpack(arg)) }));
1319 end, watchers);
1320
1321 command_runner:run(true);