Browse Source

gluon-announce: move collect_dir to library

pull/110/head
Nils Schneider 7 years ago
committed by Matthias Schiffer
parent
commit
2a33a2f498
  1. 39
      gluon/gluon-announce/files/lib/gluon/announce/collect.lua
  2. 33
      gluon/gluon-announce/files/usr/lib/lua/gluon/announce.lua

39
gluon/gluon-announce/files/lib/gluon/announce/collect.lua

@ -1,43 +1,10 @@
#!/usr/bin/lua
local announce_base = '/lib/gluon/announce/'
fs = require 'luci.fs'
uci = require('luci.model.uci').cursor()
util = require 'luci.util'
local announce = require 'gluon.announce'
local json = require 'luci.json'
local ltn12 = require 'luci.ltn12'
local announce_dir = '/lib/gluon/announce/' .. arg[1] .. '.d'
local function collect_entry(entry)
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))
encoder = json.Encoder(announce.collect_dir(announce_dir))
ltn12.pump.all(encoder:source(), ltn12.sink.file(io.stdout))

33
gluon/gluon-announce/files/usr/lib/lua/gluon/announce.lua

@ -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…
Cancel
Save