mirror of
https://github.com/vxcontrol/soldr-modules.git
synced 2026-07-19 21:43:31 -04:00
30 lines
722 B
Lua
30 lines
722 B
Lua
local function show_output(s)
|
|
local MAX_LENGTH = 1024
|
|
s = string.gsub(s, "\n", "\\n")
|
|
s = string.sub(s, 1, MAX_OUTPUT_LENGTH)
|
|
return s
|
|
end
|
|
|
|
local function trim_end(s)
|
|
return string.gsub(s, "%s+$", "")
|
|
end
|
|
|
|
-- Executes the given command in a shell.
|
|
-- NOTE: stderr is redirecting to stdout.
|
|
--:: string -> string?, err::string
|
|
local function exec(cmd)
|
|
local f = io.popen("exec 2>&1;" .. cmd)
|
|
if not f then
|
|
return false, string.format("exec: %q: io.popen returned nil", cmd, err)
|
|
end
|
|
local output = f:read("a") or ""
|
|
local ok, status, code = f:close()
|
|
if not ok then
|
|
return false, string.format(
|
|
"exec(%s): %s=%d: %s", cmd, status, code, show_output(output))
|
|
end
|
|
return trim_end(output)
|
|
end
|
|
|
|
return exec
|