Software /
code /
prosody
Comparison
util/prosodyctl/shell.lua @ 12802:4a8740e01813
Merge 0.12->trunk
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 12 Dec 2022 07:10:54 +0100 |
parent | 12573:0f4feaf9ca64 |
child | 12864:9f9633364044 |
comparison
equal
deleted
inserted
replaced
12801:ebd6b4d8bf04 | 12802:4a8740e01813 |
---|---|
2 local server = require "net.server"; | 2 local server = require "net.server"; |
3 local st = require "util.stanza"; | 3 local st = require "util.stanza"; |
4 local path = require "util.paths"; | 4 local path = require "util.paths"; |
5 local parse_args = require "util.argparse".parse; | 5 local parse_args = require "util.argparse".parse; |
6 local unpack = table.unpack or _G.unpack; | 6 local unpack = table.unpack or _G.unpack; |
7 local tc = require "util.termcolours"; | |
8 local isatty = require "util.pposix".isatty; | |
7 | 9 |
8 local have_readline, readline = pcall(require, "readline"); | 10 local have_readline, readline = pcall(require, "readline"); |
9 | 11 |
10 local adminstream = require "util.adminstream"; | 12 local adminstream = require "util.adminstream"; |
11 | 13 |
25 return io.read("*line"); | 27 return io.read("*line"); |
26 end | 28 end |
27 end | 29 end |
28 | 30 |
29 local function send_line(client, line) | 31 local function send_line(client, line) |
30 client.send(st.stanza("repl-input"):text(line)); | 32 client.send(st.stanza("repl-input", { width = os.getenv "COLUMNS" }):text(line)); |
31 end | 33 end |
32 | 34 |
33 local function repl(client) | 35 local function repl(client) |
34 local line = read_line(client.prompt_string or "prosody> "); | 36 local line = read_line(client.prompt_string or "prosody> "); |
35 if not line or line == "quit" or line == "exit" or line == "bye" then | 37 if not line or line == "quit" or line == "exit" or line == "bye" then |
62 end | 64 end |
63 | 65 |
64 local function start(arg) --luacheck: ignore 212/arg | 66 local function start(arg) --luacheck: ignore 212/arg |
65 local client = adminstream.client(); | 67 local client = adminstream.client(); |
66 local opts, err, where = parse_args(arg); | 68 local opts, err, where = parse_args(arg); |
69 local ttyout = isatty(io.stdout); | |
67 | 70 |
68 if not opts then | 71 if not opts then |
69 if err == "param-not-found" then | 72 if err == "param-not-found" then |
70 print("Unknown command-line option: "..tostring(where)); | 73 print("Unknown command-line option: "..tostring(where)); |
71 elseif err == "missing-value" then | 74 elseif err == "missing-value" then |
75 end | 78 end |
76 | 79 |
77 if arg[1] then | 80 if arg[1] then |
78 if arg[2] then | 81 if arg[2] then |
79 -- prosodyctl shell module reload foo bar.com --> module:reload("foo", "bar.com") | 82 -- prosodyctl shell module reload foo bar.com --> module:reload("foo", "bar.com") |
80 -- COMPAT Lua 5.1 doesn't have the separator argument to string.rep | 83 arg[1] = string.format("%s:%s("..string.rep("%q", #arg-2,", ")..")", unpack(arg)); |
81 arg[1] = string.format("%s:%s("..string.rep("%q, ", #arg-2):sub(1, -3)..")", unpack(arg)); | |
82 end | 84 end |
83 | 85 |
84 client.events.add_handler("connected", function() | 86 client.events.add_handler("connected", function() |
85 client.send(st.stanza("repl-input"):text(arg[1])); | 87 client.send(st.stanza("repl-input"):text(arg[1])); |
86 return true; | 88 return true; |
87 end, 1); | 89 end, 1); |
88 | 90 |
89 local errors = 0; -- TODO This is weird, but works for now. | 91 local errors = 0; -- TODO This is weird, but works for now. |
90 client.events.add_handler("received", function(stanza) | 92 client.events.add_handler("received", function(stanza) |
91 if stanza.name == "repl-output" or stanza.name == "repl-result" then | 93 if stanza.name == "repl-output" or stanza.name == "repl-result" then |
94 local dest = io.stdout; | |
92 if stanza.attr.type == "error" then | 95 if stanza.attr.type == "error" then |
93 errors = errors + 1; | 96 errors = errors + 1; |
94 io.stderr:write(stanza:get_text(), "\n"); | 97 dest = io.stderr; |
98 end | |
99 if stanza.attr.eol == "0" then | |
100 dest:write(stanza:get_text()); | |
95 else | 101 else |
96 print(stanza:get_text()); | 102 dest:write(stanza:get_text(), "\n"); |
97 end | 103 end |
98 end | 104 end |
99 if stanza.name == "repl-result" then | 105 if stanza.name == "repl-result" then |
100 os.exit(errors); | 106 os.exit(errors); |
101 end | 107 end |
116 end); | 122 end); |
117 | 123 |
118 client.events.add_handler("received", function (stanza) | 124 client.events.add_handler("received", function (stanza) |
119 if stanza.name == "repl-output" or stanza.name == "repl-result" then | 125 if stanza.name == "repl-output" or stanza.name == "repl-result" then |
120 local result_prefix = stanza.attr.type == "error" and "!" or "|"; | 126 local result_prefix = stanza.attr.type == "error" and "!" or "|"; |
121 print(result_prefix.." "..stanza:get_text()); | 127 local out = result_prefix.." "..stanza:get_text(); |
128 if ttyout and stanza.attr.type == "error" then | |
129 out = tc.getstring(tc.getstyle("red"), out); | |
130 end | |
131 print(out); | |
122 end | 132 end |
123 if stanza.name == "repl-result" then | 133 if stanza.name == "repl-result" then |
124 repl(client); | 134 repl(client); |
125 end | 135 end |
126 end); | 136 end); |