Changeset

11423:0a10bb3b129b

util.prosodyctl.shell: Allow passing a single command as argument Test procedure: $ prosodyctl shell 'server:version()' Expect: > OK: hg:926d53af9a7a $ prosodyctl shell 'server:version()' 'hello' Expect: > Only one command is supported as argument $ prosodyctl shell 'lorem ipsum'; echo $? Expect: > Sorry, I couldn't understand that... console:1: syntax error near 'show' > 1 (error code) Thanks Menel for mentioning the feature
author Kim Alvefur <zash@zash.se>
date Fri, 05 Mar 2021 13:02:37 +0100
parents 11422:fa5a23d7aabc
children 11424:2358299bc928
files util/prosodyctl/shell.lua
diffstat 1 files changed, 29 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/util/prosodyctl/shell.lua	Fri Mar 05 12:39:49 2021 +0100
+++ b/util/prosodyctl/shell.lua	Fri Mar 05 13:02:37 2021 +0100
@@ -71,6 +71,35 @@
 		os.exit(1);
 	end
 
+	if arg[1] then
+		if arg[2] then
+			-- TODO send each arg[] and wait for reply?
+			print("Only one command is supported as argument");
+			os.exit(1);
+		end
+
+		client.events.add_handler("connected", function()
+			client.send(st.stanza("repl-input"):text(arg[1]));
+			return true;
+		end, 1);
+
+		local errors = 0; -- TODO This is weird, but works for now.
+		client.events.add_handler("received", function(stanza)
+			if stanza.name == "repl-output" or stanza.name == "repl-result" then
+				if stanza.attr.type == "error" then
+					errors = errors + 1;
+					io.stderr:write(stanza:get_text(), "\n");
+				else
+					print(stanza:get_text());
+				end
+			end
+			if stanza.name == "repl-result" then
+				os.exit(errors);
+			end
+			return true;
+		end, 1);
+	end
+
 	client.events.add_handler("connected", function ()
 		if not opts.quiet then
 			printbanner();