64 lines
1.9 KiB
Lua
64 lines
1.9 KiB
Lua
local function dbg(item)
|
|
local function indent(indentation)
|
|
return string.rep(" ", indentation)
|
|
end
|
|
|
|
local function dbg_indent(item_, indentation)
|
|
local result = ""
|
|
if type(item_) == "table" then
|
|
result = "{"
|
|
for key, value in pairs(item_) do
|
|
result = result
|
|
.. "\n" .. indent(indentation + 1)
|
|
.. dbg_indent(key, indentation + 1) .. ": "
|
|
.. dbg_indent(value, indentation + 1)
|
|
end
|
|
result = result
|
|
.. "\n" .. indent(indentation) .. "}"
|
|
elseif type(item_) == "string" then
|
|
result = "\"" .. item_ .. "\""
|
|
else
|
|
result = tostring(item_)
|
|
end
|
|
return result
|
|
end
|
|
|
|
print(dbg_indent(item, 0))
|
|
return item
|
|
end
|
|
|
|
local function new_config(name)
|
|
local path = "/etc/" .. name
|
|
|
|
local function read_key_in_line(line, key)
|
|
local prefix = key .. ":"
|
|
local prefix_len = string.len(prefix)
|
|
local actual = string.sub(line, 0, prefix_len)
|
|
if not (prefix == actual) then return nil end
|
|
local value = string.sub(line, prefix_len + 1)
|
|
return value
|
|
end
|
|
|
|
local function get(key, default)
|
|
local file = fs.open(path, "r")
|
|
if file == nil then return default end
|
|
while true do
|
|
local line = file.readLine()
|
|
if line == nil then return default end
|
|
local value = read_key_in_line(line, key)
|
|
if not (value == nil) then return value end
|
|
end
|
|
end
|
|
|
|
local function get_num(key, default)
|
|
local str = get(key, nil)
|
|
if str == nil then return default end
|
|
local result = tonumber(str)
|
|
if result == nil then
|
|
error("Tried to parse conf '" .. path .. "'\nkey \"" .. key .. "\": \"" .. str .. "\" as number.")
|
|
end
|
|
return result
|
|
end
|
|
|
|
return { get = get, get_num = get_num }
|
|
end
|