gluon-announce: move collect_dir to library
This commit is contained in:
parent
8361b51c32
commit
7e7ee16105
39
package/gluon-announce/files/lib/gluon/announce/collect.lua
Executable file → Normal file
39
package/gluon-announce/files/lib/gluon/announce/collect.lua
Executable file → Normal 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))
|
||||||
|
33
package/gluon-announce/files/usr/lib/lua/gluon/announce.lua
Executable file
33
package/gluon-announce/files/usr/lib/lua/gluon/announce.lua
Executable 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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user