Skip to content

Instantly share code, notes, and snippets.

@fur-q
Last active December 14, 2015 06:59
Show Gist options
  • Save fur-q/5047000 to your computer and use it in GitHub Desktop.
Save fur-q/5047000 to your computer and use it in GitHub Desktop.
the best lua config loader idst
hostname = "localhost"
reconnect = true
attempts = 10
ignore = { "user1", "user2" }
channels = {}
for i = 1, 4 do
channels[i] = "#channel" .. i
end
include "config2.cfg"
local function include(name, env)
local cfg, err = assert(loadfile(name, "t", env))
if setfenv then
setfenv(cfg, env)
end
cfg()
return env
end
local function load(name, env)
env = setmetatable(env or {},
{ __index = { include = function(f) return include(f, env) end } }
)
local ok, env = xpcall(function()
debug.sethook(function() error("timed out") end, "c", 1e5)
include(name, env)
debug.sethook()
end, debug.traceback)
return ok and env or nil, ok or env
end
return { load = load }
local config = require "config"
local cfg = config.load("config.cfg")
print(cfg.hostname, cfg.attempts, cfg.channels[1])
--> localhost 10 #channel1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment