changeset 4421:94805a7e7b30

mod_invites: rework CLI parsing to support groups To make this sensible, the code had to move from rather simple parsing to something which looks more like getopt or your typical shell script.
author Jonas Schäfer <jonas@wielicki.name>
date Sun, 31 Jan 2021 19:16:36 +0100
parents a81516906822
children 2047dd56cc40
files mod_invites/mod_invites.lua
diffstat 1 files changed, 52 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/mod_invites/mod_invites.lua	Sun Jan 31 16:12:47 2021 +0100
+++ b/mod_invites/mod_invites.lua	Sun Jan 31 19:16:36 2021 +0100
@@ -234,7 +234,7 @@
 function module.command(arg)
 	if #arg < 2 or arg[1] ~= "generate" then
 		print("usage: prosodyctl mod_"..module.name.." generate example.com");
-		return;
+		return 2;
 	end
 	table.remove(arg, 1); -- pop command
 
@@ -255,23 +255,60 @@
 		module:depends(invites_page_module);
 	end
 
+	local allow_reset;
+	local roles;
+	local groups = {};
 
-	local invite, roles;
-	if arg[1] == "--reset" then
-		local nodeprep = require "util.encodings".stringprep.nodeprep;
-		local username = nodeprep(arg[2]);
-		if not username then
-			print("Please supply a valid username to generate a reset link for");
-			return;
+	while #arg > 0 do
+		local value = arg[1];
+		table.remove(arg, 1);
+		if value == "--reset" then
+			local nodeprep = require "util.encodings".stringprep.nodeprep;
+			local username = nodeprep(arg[1])
+			table.remove(arg, 1);
+			if not username then
+				print("Please supply a valid username to generate a reset link for");
+				return 2;
+			end
+			allow_reset = username;
+		elseif value == "--admin" then
+			roles = { ["prosody:admin"] = true };
+		elseif value == "--role" then
+			local rolename = arg[1];
+			if not rolename then
+				print("Please supply a role name");
+				return 2;
+			end
+			roles = { [rolename] = true };
+			table.remove(arg, 1);
+		elseif value == "--group" or value == "-g" then
+			local groupid = arg[1];
+			if not groupid then
+				print("Please supply a group ID")
+				return 2;
+			end
+			table.insert(groups, groupid);
+			table.remove(arg, 1);
+		else
+			print("unexpected argument: "..value)
 		end
-		invite = assert(invites.create_account_reset(username));
+	end
+
+	local invite;
+	if allow_reset then
+		if roles then
+			print("--role/--admin and --reset are mutually exclusive")
+			return 2;
+		end
+		if #groups > 0 then
+			print("--group and --reset are mutually exclusive")
+		end
+		invite = assert(invites.create_account_reset(allow_reset));
 	else
-		if arg[1] == "--admin" then
-			roles = { ["prosody:admin"] = true };
-		elseif arg[1] == "--role" then
-			roles = { [arg[2]] = true };
-		end
-		invite = assert(invites.create_account(nil, { roles = roles }));
+		invite = assert(invites.create_account(nil, {
+			roles = roles,
+			groups = groups
+		}));
 	end
 
 	print(invite.landing_page or invite.uri);