gluon-announce: move collect_dir to library

This commit is contained in:
Nils Schneider 2015-03-23 21:04:27 +01:00 committed by Matthias Schiffer
parent 8361b51c32
commit 7e7ee16105
2 changed files with 36 additions and 36 deletions

View File

@ -1,43 +1,10 @@
#!/usr/bin/lua #!/usr/bin/lua
local announce_base = '/lib/gluon/announce/' local announce = require 'gluon.announce'
fs = require 'luci.fs'
uci = require('luci.model.uci').cursor()
util = require 'luci.util'
local json = require 'luci.json' local json = require 'luci.json'
local ltn12 = require 'luci.ltn12' local ltn12 = require 'luci.ltn12'
local announce_dir = '/lib/gluon/announce/' .. arg[1] .. '.d'
local function collect_entry(entry) encoder = json.Encoder(announce.collect_dir(announce_dir))
if fs.isdirectory(entry) then
return collect_dir(entry)
else
return dofile(entry)
end
end
function collect_dir(dir)
local ret = {}
for _, entry in ipairs(fs.dir(dir)) do
if entry:sub(1, 1) ~= '.' then
local ok, val = pcall(collect_entry, dir .. '/' .. entry)
if ok then
ret[entry] = val
else
io.stderr:write(val, '\n')
end
end
end
return ret
end
local announce_dir = announce_base .. arg[1] .. '.d/'
encoder = json.Encoder(collect_dir(announce_dir))
ltn12.pump.all(encoder:source(), ltn12.sink.file(io.stdout)) ltn12.pump.all(encoder:source(), ltn12.sink.file(io.stdout))

View File

@ -0,0 +1,33 @@
#!/usr/bin/lua
module('gluon.announce', package.seeall)
fs = require 'luci.fs'
uci = require('luci.model.uci').cursor()
util = require 'luci.util'
local function collect_entry(entry)
if fs.isdirectory(entry) then
return collect_dir(entry)
else
return setfenv(loadfile(entry), _M)()
end
end
function collect_dir(dir)
local ret = {}
for _, entry in ipairs(fs.dir(dir)) do
if entry:sub(1, 1) ~= '.' then
local ok, val = pcall(collect_entry, dir .. '/' .. entry)
if ok then
ret[entry] = val
else
io.stderr:write(val, '\n')
end
end
end
return ret
end