36 lines
1.0 KiB
Lua
36 lines
1.0 KiB
Lua
local cmd = {}
|
|
|
|
-- Execute a command, redirecting stdout to stderr to avoid polluting our JSON output
|
|
function cmd.execute(command)
|
|
-- Redirect stdout to stderr so only our JSON goes to stdout
|
|
local redirected_cmd = command .. " 1>&2"
|
|
return os.execute(redirected_cmd)
|
|
end
|
|
|
|
-- Execute a command and capture its output
|
|
function cmd.read(command)
|
|
local handle = io.popen(command .. " 2>&1")
|
|
local result = handle:read("*a")
|
|
local success = handle:close()
|
|
return result, success
|
|
end
|
|
|
|
-- Execute a command and capture stdout separately from stderr
|
|
function cmd.read_stdout(command)
|
|
local handle = io.popen(command .. " 2>/dev/null")
|
|
local result = handle:read("*a")
|
|
local success = handle:close()
|
|
return result, success
|
|
end
|
|
|
|
-- Execute a command and return only the exit status
|
|
function cmd.run(command)
|
|
return os.execute(command .. " >/dev/null 2>&1")
|
|
end
|
|
|
|
-- Check if a command exists
|
|
function cmd.exists(command_name)
|
|
return cmd.run("command -v " .. command_name) == 0
|
|
end
|
|
|
|
return cmd |