]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
daemon/lua: made ‘modules’ append-only
authorMarek Vavruša <marek.vavrusa@nic.cz>
Tue, 31 Mar 2015 20:47:52 +0000 (22:47 +0200)
committerMarek Vavruša <marek.vavrusa@nic.cz>
Tue, 31 Mar 2015 20:47:52 +0000 (22:47 +0200)
this makes some services append-only, so the configuration
can look like:

modules = {
   cache = ‘’,
   hints = true,
}

without wiping previously-set modules

daemon/lua/init.lua

index 6c7a170d7abf6ed8e4b61de3f527072d4e418b43..e71b08720aa8fa0f7dec5644734a895d5572e2c2 100644 (file)
@@ -1,9 +1,32 @@
 -- Syntactic sugar for module loading
 -- `modules.<name> = <config>`
-local modules_mt = {
+setmetatable(modules, {
        __newindex = function (t,k,v)
                modules.load(k)
-               _G[k]['config'](v)
+               if _G[k] then
+                       local config_call = _G[k]['config']
+                       if config_call and config_call[''] then
+                               config_call(v)
+                       end
+               end
        end
-}
-setmetatable(modules, modules_mt)
\ No newline at end of file
+})
+
+-- Some services are append-only
+function protect(defined)
+       local __protected = { ['modules'] = true }
+       return setmetatable({}, {
+               __index = defined,
+               __newindex = function (t, k, v)
+                       if __protected[k] then
+                               for k2,v2 in pairs(v) do
+                                       defined[k][k2] = v2
+                               end
+                       else
+                               defined[k] = v
+                       end
+               end
+       })
+end
+_G = protect(getfenv(0))
+setfenv(0, _G)