diff options
-rw-r--r-- | src/ffluci/cbi.lua | 17 | ||||
-rw-r--r-- | src/ffluci/fs.lua | 17 | ||||
-rw-r--r-- | src/ffluci/model/uci.lua | 86 | ||||
-rw-r--r-- | src/ffluci/template.lua | 18 | ||||
-rw-r--r-- | src/ffluci/util.lua | 10 |
5 files changed, 105 insertions, 43 deletions
diff --git a/src/ffluci/cbi.lua b/src/ffluci/cbi.lua index 0c2718ea8e..a535dc8b72 100644 --- a/src/ffluci/cbi.lua +++ b/src/ffluci/cbi.lua @@ -41,10 +41,10 @@ function load(cbimap) require("ffluci.i18n") local cbidir = ffluci.fs.dirname(ffluci.util.__file__()) .. "model/cbi/" - local func = loadfile(cbidir..cbimap..".lua") + local func, err = loadfile(cbidir..cbimap..".lua") if not func then - error("Unable to load CBI map: " .. cbimap) + error(err) return nil end @@ -102,10 +102,11 @@ function Map.__init__(self, config, ...) Node.__init__(self, ...) self.config = config self.template = "cbi/map" + self.uci = ffluci.model.uci.Session() end function Map.parse(self) - self.ucidata = ffluci.model.uci.show(self.config) + self.ucidata = self.uci:show(self.config) if not self.ucidata then error("Unable to read UCI data: " .. self.config) else @@ -115,7 +116,7 @@ function Map.parse(self) end function Map.render(self) - self.ucidata = ffluci.model.uci.show(self.config) + self.ucidata = self.uci:show(self.config) if not self.ucidata then error("Unable to read UCI data: " .. self.config) else @@ -136,6 +137,10 @@ function Map.section(self, class, ...) end end +function Map.set(self, section, option, value) + return self.uci:set(self.config, section, option, value) +end + --[[ AbstractSection ]]-- @@ -262,7 +267,7 @@ function AbstractValue.validate(self, value) end function AbstractValue.write(self, value) - return ffluci.model.uci.set(self.config, self.section, self.option, value) + return self.map:set(self.section, self.option, value) end @@ -296,7 +301,7 @@ function ListValue.__init__(self, ...) self.list = {} end -function ListValue.addValue(self, key, val) +function ListValue.add_value(self, key, val) val = val or key self.list[key] = val end
\ No newline at end of file diff --git a/src/ffluci/fs.lua b/src/ffluci/fs.lua index fb9a9c1c95..fdea1b51ec 100644 --- a/src/ffluci/fs.lua +++ b/src/ffluci/fs.lua @@ -37,10 +37,10 @@ end -- Returns the content of file function readfile(filename) - local fp = io.open(filename) + local fp, err = io.open(filename) if fp == nil then - error("Unable to open file for reading: " .. filename) + error(err) end local data = fp:read("*a") @@ -50,12 +50,12 @@ end -- Returns the content of file as array of lines function readfilel(filename) - local fp = io.open(filename) + local fp, err = io.open(filename) local line = "" local data = {} if fp == nil then - error("Unable to open file for reading: " .. filename) + error(err) end while true do @@ -70,9 +70,9 @@ end -- Writes given data to a file function writefile(filename, data) - local fp = io.open(filename, "w") + local fp, err = io.open(filename, "w") if fp == nil then - error("Unable to open file for writing: " .. filename) + error(err) end fp:write(data) fp:close() @@ -97,4 +97,9 @@ function dir(path) end end return e +end + +-- Alias for lfs.mkdir +function mkdir(...) + return lfs.mkdir(...) end
\ No newline at end of file diff --git a/src/ffluci/model/uci.lua b/src/ffluci/model/uci.lua index 9b9fcb0935..5faffe5486 100644 --- a/src/ffluci/model/uci.lua +++ b/src/ffluci/model/uci.lua @@ -28,55 +28,101 @@ limitations under the License. ]]-- module("ffluci.model.uci", package.seeall) require("ffluci.util") +require("ffluci.fs") +-- The OS uci command ucicmd = "uci" +-- Session class +Session = ffluci.util.class() + +-- Session constructor +function Session.__init__(self, path, uci) + uci = uci or ucicmd + if path then + self.ucicmd = uci .. " -P " .. path + else + self.ucicmd = uci + end +end + +-- The default Session +local default = Session() + -- Wrapper for "uci add" -function add(config, section_type) - return _uci("add " .. _path(config) .. " " .. _path(section_type)) +function Session.add(self, config, section_type) + return self:_uci("add " .. _path(config) .. " " .. _path(section_type)) +end + +function add(...) + return default:add(...) end -- Wrapper for "uci changes" -function changes(config) - return _uci3("changes " .. _path(config)) +function Session.changes(self, config) + return self:_uci3("changes " .. _path(config)) +end + +function change(...) + return default:change(...) end -- Wrapper for "uci commit" -function commit(config) - return _uci2("commit " .. _path(config)) +function Session.commit(self, config) + return self:_uci2("commit " .. _path(config)) +end + +function commit(...) + return default:commit(...) end -- Wrapper for "uci get" -function get(config, section, option) - return _uci("get " .. _path(config, section, option)) +function Session.get(self, config, section, option) + return self:_uci("get " .. _path(config, section, option)) +end + +function get(...) + return default:get(...) end -- Wrapper for "uci revert" -function revert(config) - return _uci2("revert " .. _path(config)) +function Session.revert(self, config) + return self:_uci2("revert " .. _path(config)) +end + +function revert(...) + return self:revert(...) end -- Wrapper for "uci show" -function show(config) - return _uci3("show " .. _path(config)) +function Session.show(self, config) + return self:_uci3("show " .. _path(config)) +end + +function show(...) + return default:show(...) end -- Wrapper for "uci set" -function set(config, section, option, value) - return _uci2("set " .. _path(config, section, option, value)) +function Session.set(self, config, section, option, value) + return self:_uci2("set " .. _path(config, section, option, value)) +end + +function set(...) + return default:set(...) end -- Internal functions -- -function _uci(cmd) - local res = ffluci.util.exec(ucicmd .. " 2>/dev/null " .. cmd) +function Session._uci(self, cmd) + local res = ffluci.util.exec(self.ucicmd .. " 2>/dev/null " .. cmd) if res:len() == 0 then return nil @@ -85,8 +131,8 @@ function _uci(cmd) end end -function _uci2(cmd) - local res = ffluci.util.exec(ucicmd .. " 2>&1 " .. cmd) +function Session._uci2(self, cmd) + local res = ffluci.util.exec(self.ucicmd .. " 2>&1 " .. cmd) if res:len() > 0 then return false, res @@ -95,8 +141,8 @@ function _uci2(cmd) end end -function _uci3(cmd) - local res = ffluci.util.execl(ucicmd .. " 2>&1 " .. cmd) +function Session._uci3(self, cmd) + local res = ffluci.util.execl(self.ucicmd .. " 2>&1 " .. cmd) if res[1]:sub(1, ucicmd:len() + 1) == ucicmd .. ":" then return nil, res[1] end diff --git a/src/ffluci/template.lua b/src/ffluci/template.lua index 18265a4a16..17f36194fd 100644 --- a/src/ffluci/template.lua +++ b/src/ffluci/template.lua @@ -128,7 +128,7 @@ function render(name, scope, ...) scope = scope or getfenv(2) local s, t = pcall(Template, name) if not s then - error("Unable to load template: " .. name) + error(t) else t:render(scope, ...) end @@ -165,7 +165,8 @@ function Template.__init__(self, name) -- Compile and build local sourcefile = viewdir .. name .. ".htm" - local compiledfile = viewdir .. name .. ".lua" + local compiledfile = viewdir .. name .. ".lua" + local err if compiler_mode == "file" then local tplmt = ffluci.fs.mtime(sourcefile) @@ -176,25 +177,22 @@ function Template.__init__(self, name) or (not (commt == nil) and not (tplmt == nil) and commt < tplmt) then local compiled = compile(ffluci.fs.readfile(sourcefile)) ffluci.fs.writefile(compiledfile, compiled) - self.template = loadstring(compiled) + self.template, err = loadstring(compiled) else - self.template = loadfile(compiledfile) + self.template, err = loadfile(compiledfile) end elseif compiler_mode == "none" then - self.template = loadfile(self.compiledfile) + self.template, err = loadfile(self.compiledfile) elseif compiler_mode == "memory" then - self.template = loadstring(compile(ffluci.fs.readfile(sourcefile))) + self.template, err = loadstring(compile(ffluci.fs.readfile(sourcefile))) - else - error("Invalid compiler mode: " .. compiler_mode) - end -- If we have no valid template throw error, otherwise cache the template if not self.template then - error("Unable to load template: " .. name) + error(err) else self.cache[name] = self.template end diff --git a/src/ffluci/util.lua b/src/ffluci/util.lua index 8a526173d7..38f33a20d4 100644 --- a/src/ffluci/util.lua +++ b/src/ffluci/util.lua @@ -36,7 +36,10 @@ function class(base) setmetatable(inst, {__index = class}) if inst.__init__ then - inst:__init__(...) + local stat, err = pcall(inst.__init__, inst, ...) + if not stat then + error(err) + end end return inst @@ -138,6 +141,11 @@ function resfenv(f) end +-- Returns the Haserl unique sessionid +function sessionid() + return ENV.SESSIONID +end + -- Updates the scope of f with "extscope" function updfenv(f, extscope) local scope = getfenv(f) |