From cd641dca64af1b47fb71235a4915ab7c91b4cf90 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 5 Mar 2021 13:02:37 +0100 Subject: 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 --- util/prosodyctl/shell.lua | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'util/prosodyctl') diff --git a/util/prosodyctl/shell.lua b/util/prosodyctl/shell.lua index d6da9dab..a05904f0 100644 --- a/util/prosodyctl/shell.lua +++ b/util/prosodyctl/shell.lua @@ -71,6 +71,35 @@ local function start(arg) --luacheck: ignore 212/arg 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(); -- cgit v1.2.3