mirror of
https://github.com/MightyPirates/OpenComputers.git
synced 2025-09-14 09:46:53 -04:00
Improved rc script system
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
parent
e1fe125989
commit
20bb0a062d
@ -1,8 +1,8 @@
|
|||||||
local rc = require('rc')
|
local rc = require('rc')
|
||||||
|
|
||||||
local args = table.pack(...)
|
local args = table.pack(...)
|
||||||
if args.n < 2 then
|
if args.n < 1 then
|
||||||
io.write("Usage: rc <service> <command> [args...]")
|
io.write("Usage: rc <service> [command] [args...]")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -11,3 +11,4 @@ local result, reason = rc.runCommand(table.unpack(args))
|
|||||||
if not result then
|
if not result then
|
||||||
io.stderr:write(reason .. "\n")
|
io.stderr:write(reason .. "\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
local fs = require('filesystem')
|
local fs = require('filesystem')
|
||||||
|
local serialization = require('serialization')
|
||||||
|
|
||||||
-- Keeps track of loaded scripts to retain local values between invocation
|
-- Keeps track of loaded scripts to retain local values between invocation
|
||||||
-- of their command callbacks.
|
-- of their command callbacks.
|
||||||
@ -18,6 +19,19 @@ local function loadConfig()
|
|||||||
return nil, reason
|
return nil, reason
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function saveConfig(conf)
|
||||||
|
local file, reason = io.open('/etc/rc.cfg', 'w')
|
||||||
|
if not file then
|
||||||
|
return nil, reason
|
||||||
|
end
|
||||||
|
for key, value in pairs(conf) do
|
||||||
|
file:write(tostring(key) .. " = " .. serialization.serialize(value) .. "\n")
|
||||||
|
end
|
||||||
|
|
||||||
|
file:close()
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
function rc.load(name, args)
|
function rc.load(name, args)
|
||||||
if loaded[name] then
|
if loaded[name] then
|
||||||
return loaded[name]
|
return loaded[name]
|
||||||
@ -39,14 +53,47 @@ function rc.unload(name)
|
|||||||
loaded[name] = nil
|
loaded[name] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
local function rawRunCommand(name, cmd, args, ...)
|
local function rawRunCommand(conf, name, cmd, args, ...)
|
||||||
local result, what = rc.load(name, args)
|
local result, what = rc.load(name, args)
|
||||||
if result then
|
if result then
|
||||||
if type(result[cmd]) == "function" then
|
if not cmd then
|
||||||
result, what = xpcall(result[cmd], debug.traceback, ...)
|
io.output():write("Commands for service " .. name .. "\n")
|
||||||
if result then
|
for command, val in pairs(result) do
|
||||||
|
if type(val) == "function" then
|
||||||
|
io.output():write(tostring(command) .. " ")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return true
|
||||||
|
elseif type(result[cmd]) == "function" then
|
||||||
|
res, what = xpcall(result[cmd], debug.traceback, ...)
|
||||||
|
if res then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
elseif cmd == "restart" and type(result["stop"]) == "function" and type(result["start"]) == "function" then
|
||||||
|
res, what = xpcall(result["stop"], debug.traceback, ...)
|
||||||
|
if res then
|
||||||
|
res, what = xpcall(result["start"], debug.traceback, ...)
|
||||||
|
if res then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
elseif cmd == "enable" then
|
||||||
|
conf.enabled = conf.enabled or {}
|
||||||
|
for _, _name in ipairs(conf.enabled) do
|
||||||
|
if name == _name then
|
||||||
|
return nil, "Service already enabled"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
conf.enabled[#conf.enabled + 1] = name
|
||||||
|
return saveConfig(conf)
|
||||||
|
elseif cmd == "disable" then
|
||||||
|
conf.enabled = conf.enabled or {}
|
||||||
|
for n, _name in ipairs(conf.enabled) do
|
||||||
|
if name == _name then
|
||||||
|
table.remove(conf.enabled, n)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return saveConfig(conf)
|
||||||
else
|
else
|
||||||
what = "Command '" .. cmd .. "' not found in daemon '" .. name .. "'"
|
what = "Command '" .. cmd .. "' not found in daemon '" .. name .. "'"
|
||||||
end
|
end
|
||||||
@ -59,7 +106,7 @@ function rc.runCommand(name, cmd, ...)
|
|||||||
if not conf then
|
if not conf then
|
||||||
return nil, reason
|
return nil, reason
|
||||||
end
|
end
|
||||||
return rawRunCommand(name, cmd, conf[name], ...)
|
return rawRunCommand(conf, name, cmd, conf[name], ...)
|
||||||
end
|
end
|
||||||
|
|
||||||
function rc.allRunCommand(cmd, ...)
|
function rc.allRunCommand(cmd, ...)
|
||||||
@ -69,9 +116,10 @@ function rc.allRunCommand(cmd, ...)
|
|||||||
end
|
end
|
||||||
local results = {}
|
local results = {}
|
||||||
for _, name in ipairs(conf.enabled or {}) do
|
for _, name in ipairs(conf.enabled or {}) do
|
||||||
results[name] = table.pack(rawRunCommand(name, cmd, conf[name], ...))
|
results[name] = table.pack(rawRunCommand(conf, name, cmd, conf[name], ...))
|
||||||
end
|
end
|
||||||
return results
|
return results
|
||||||
end
|
end
|
||||||
|
|
||||||
return rc
|
return rc
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user