From 7e7ee16105f6922581ca1eb0cf53268e7e3ae28f Mon Sep 17 00:00:00 2001 From: Nils Schneider Date: Mon, 23 Mar 2015 21:04:27 +0100 Subject: [PATCH] gluon-announce: move collect_dir to library --- .../files/lib/gluon/announce/collect.lua | 39 ++----------------- .../files/usr/lib/lua/gluon/announce.lua | 33 ++++++++++++++++ 2 files changed, 36 insertions(+), 36 deletions(-) mode change 100755 => 100644 package/gluon-announce/files/lib/gluon/announce/collect.lua create mode 100755 package/gluon-announce/files/usr/lib/lua/gluon/announce.lua diff --git a/package/gluon-announce/files/lib/gluon/announce/collect.lua b/package/gluon-announce/files/lib/gluon/announce/collect.lua old mode 100755 new mode 100644 index 7bce0a78..e2974b49 --- a/package/gluon-announce/files/lib/gluon/announce/collect.lua +++ b/package/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)) diff --git a/package/gluon-announce/files/usr/lib/lua/gluon/announce.lua b/package/gluon-announce/files/usr/lib/lua/gluon/announce.lua new file mode 100755 index 00000000..7f3f62e2 --- /dev/null +++ b/package/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 +