make use of the new features in the binding for uci v0.4.0 - fixes remaining dependencies of libuci.lua on the cli
This commit is contained in:
parent
3bcf6dbea3
commit
77f8074a01
8 changed files with 58 additions and 97 deletions
|
@ -4,7 +4,7 @@ local util = require("luci.util")
|
||||||
local sys = require("luci.sys")
|
local sys = require("luci.sys")
|
||||||
local fs = require("luci.fs")
|
local fs = require("luci.fs")
|
||||||
local uci = require("luci.model.uci").Session()
|
local uci = require("luci.model.uci").Session()
|
||||||
local sections, names = uci:sections( "luci_statistics" )
|
local sections = uci:sections( "luci_statistics" )
|
||||||
|
|
||||||
|
|
||||||
Instance = util.class()
|
Instance = util.class()
|
||||||
|
|
|
@ -17,7 +17,7 @@ function Graph.__init__( self, timespan, opts )
|
||||||
opts = opts or { }
|
opts = opts or { }
|
||||||
|
|
||||||
local uci = luci.model.uci.Session()
|
local uci = luci.model.uci.Session()
|
||||||
local sections, names = uci:sections( "luci_statistics" )
|
local sections = uci:sections( "luci_statistics" )
|
||||||
|
|
||||||
-- helper classes
|
-- helper classes
|
||||||
self.colors = luci.statistics.rrdtool.colors.Instance()
|
self.colors = luci.statistics.rrdtool.colors.Instance()
|
||||||
|
|
|
@ -22,7 +22,7 @@ require("luci.util")
|
||||||
|
|
||||||
local ipt = luci.sys.iptparser.IptParser()
|
local ipt = luci.sys.iptparser.IptParser()
|
||||||
local uci = luci.model.uci.Session()
|
local uci = luci.model.uci.Session()
|
||||||
local sections, names = uci:sections( "luci_statistics" )
|
local sections = uci:sections( "luci_statistics" )
|
||||||
|
|
||||||
|
|
||||||
function section( plugin )
|
function section( plugin )
|
||||||
|
|
|
@ -89,4 +89,4 @@ end
|
||||||
-- Wrapper for "uci set"
|
-- Wrapper for "uci set"
|
||||||
function set(...)
|
function set(...)
|
||||||
return default:set(...)
|
return default:set(...)
|
||||||
end
|
end
|
||||||
|
|
|
@ -35,44 +35,43 @@ Session = luci.util.class()
|
||||||
|
|
||||||
-- Session constructor
|
-- Session constructor
|
||||||
function Session.__init__(self, savedir)
|
function Session.__init__(self, savedir)
|
||||||
self.ucicmd = savedir and "uci -P " .. savedir or "uci"
|
|
||||||
self.savedir = savedir or luci.model.uci.savedir
|
self.savedir = savedir or luci.model.uci.savedir
|
||||||
|
uci.set_savedir(self.savedir)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Session.add(self, config, section_type)
|
function Session.add(self, config, section_type)
|
||||||
return self:_uci("add " .. _path(config) .. " " .. _path(section_type))
|
return uci.add(config, section_type)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Session.changes(self, config)
|
function Session.changes(self, config)
|
||||||
return self:_uci("changes " .. _path(config))
|
if config then
|
||||||
|
return uci.changes(config)
|
||||||
|
else
|
||||||
|
return uci.changes()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Session.commit(self, config)
|
function Session.commit(self, config)
|
||||||
self:t_load(config)
|
|
||||||
return self:t_commit(config)
|
return self:t_commit(config)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Session.del(self, config, section, option)
|
function Session.del(self, config, section, option)
|
||||||
return self:_uci2("del " .. _path(config, section, option))
|
return uci.del(config, section, option)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Session.get(self, config, section, option)
|
function Session.get(self, config, section, option)
|
||||||
self:t_load(config)
|
|
||||||
return self:t_get(config, section, option)
|
return self:t_get(config, section, option)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Session.revert(self, config)
|
function Session.revert(self, config)
|
||||||
self:t_load(config)
|
|
||||||
return self:t_revert(config)
|
return self:t_revert(config)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Session.sections(self, config)
|
function Session.sections(self, config)
|
||||||
self:t_load(config)
|
|
||||||
return self:t_sections(config)
|
return self:t_sections(config)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Session.set(self, config, section, option, value)
|
function Session.set(self, config, section, option, value)
|
||||||
self:t_load(config)
|
|
||||||
return self:t_set(config, section, option, value) and self:t_save(config)
|
return self:t_set(config, section, option, value) and self:t_save(config)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -92,10 +91,7 @@ function Session.t_save(self, config)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Session.t_add(self, config, type)
|
function Session.t_add(self, config, type)
|
||||||
self:t_save(config)
|
return self:add(config, type)
|
||||||
local r = self:add(config, type)
|
|
||||||
self:t_load(config)
|
|
||||||
return r
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function Session.t_commit(self, config)
|
function Session.t_commit(self, config)
|
||||||
|
@ -103,10 +99,7 @@ function Session.t_commit(self, config)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Session.t_del(self, config, section, option)
|
function Session.t_del(self, config, section, option)
|
||||||
self:t_save(config)
|
return self:del(config, section, option)
|
||||||
local r = self:del(config, section, option)
|
|
||||||
self:t_load(config)
|
|
||||||
return r
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function Session.t_get(self, config, section, option)
|
function Session.t_get(self, config, section, option)
|
||||||
|
@ -122,72 +115,14 @@ function Session.t_revert(self, config)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Session.t_sections(self, config)
|
function Session.t_sections(self, config)
|
||||||
local raw = uci.get_all(config)
|
return uci.get_all(config)
|
||||||
if not raw then
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
|
|
||||||
local s = {}
|
|
||||||
local o = {}
|
|
||||||
|
|
||||||
for i, sec in ipairs(raw) do
|
|
||||||
table.insert(o, sec.name)
|
|
||||||
|
|
||||||
s[sec.name] = sec.options
|
|
||||||
s[sec.name][".type"] = sec.type
|
|
||||||
end
|
|
||||||
|
|
||||||
return s, o
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function Session.t_set(self, config, section, option, value)
|
function Session.t_set(self, config, section, option, value)
|
||||||
if option then
|
if option then
|
||||||
return uci.set(config.."."..section.."."..option.."="..value)
|
return uci.set(config, section, option, value)
|
||||||
else
|
else
|
||||||
return uci.set(config.."."..section.."="..value)
|
return uci.set(config, section, value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Internal functions --
|
|
||||||
|
|
||||||
|
|
||||||
function Session._uci(self, cmd)
|
|
||||||
local res = luci.sys.exec(self.ucicmd .. " 2>/dev/null " .. cmd)
|
|
||||||
|
|
||||||
if res:len() == 0 then
|
|
||||||
return nil
|
|
||||||
else
|
|
||||||
return res:sub(1, res:len()-1)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function Session._uci2(self, cmd)
|
|
||||||
local res = luci.sys.exec(self.ucicmd .. " 2>&1 " .. cmd)
|
|
||||||
|
|
||||||
if res:len() > 0 then
|
|
||||||
return false, res
|
|
||||||
else
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Build path (config.section.option=value) and prevent command injection
|
|
||||||
function _path(...)
|
|
||||||
local result = ""
|
|
||||||
|
|
||||||
-- Not using ipairs because it is not reliable in case of nil arguments
|
|
||||||
arg.n = nil
|
|
||||||
for k,v in pairs(arg) do
|
|
||||||
if v then
|
|
||||||
v = tostring(v)
|
|
||||||
if k == 1 then
|
|
||||||
result = "'" .. v:gsub("['.]", "") .. "'"
|
|
||||||
elseif k < 4 then
|
|
||||||
result = result .. ".'" .. v:gsub("['.]", "") .. "'"
|
|
||||||
elseif k == 4 then
|
|
||||||
result = result .. "='" .. v:gsub("'", "") .. "'"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return result
|
|
||||||
end
|
|
|
@ -8,6 +8,29 @@ function index()
|
||||||
node("admin", "uci", "apply").target = call("action_apply")
|
node("admin", "uci", "apply").target = call("action_apply")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function convert_changes(changes)
|
||||||
|
local ret = {}
|
||||||
|
for r, tbl in pairs(changes) do
|
||||||
|
for s, os in pairs(tbl) do
|
||||||
|
for o, v in pairs(os) do
|
||||||
|
local val, str
|
||||||
|
if (v == "") then
|
||||||
|
str = "-"
|
||||||
|
val = ""
|
||||||
|
else
|
||||||
|
str = ""
|
||||||
|
val = "="..v
|
||||||
|
end
|
||||||
|
str = str.."."..r
|
||||||
|
if o ~= ".type" then
|
||||||
|
str = str.."."..o
|
||||||
|
end
|
||||||
|
table.insert(ret, str..val)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- This function has a higher priority than the admin_uci/apply template
|
-- This function has a higher priority than the admin_uci/apply template
|
||||||
function action_apply()
|
function action_apply()
|
||||||
local changes = luci.model.uci.changes()
|
local changes = luci.model.uci.changes()
|
||||||
|
@ -18,8 +41,7 @@ function action_apply()
|
||||||
local run = {}
|
local run = {}
|
||||||
|
|
||||||
-- Collect files to be applied and commit changes
|
-- Collect files to be applied and commit changes
|
||||||
for i, line in ipairs(luci.util.split(changes)) do
|
for r, tbl in pairs(changes) do
|
||||||
local r = line:match("^-?([^.]+)")
|
|
||||||
if r then
|
if r then
|
||||||
com[r] = true
|
com[r] = true
|
||||||
|
|
||||||
|
@ -40,7 +62,8 @@ function action_apply()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
luci.template.render("admin_uci/apply", {changes=changes, output=output})
|
|
||||||
|
luci.template.render("admin_uci/apply", {changes=convert_changes(changes), output=output})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -50,8 +73,7 @@ function action_revert()
|
||||||
local revert = {}
|
local revert = {}
|
||||||
|
|
||||||
-- Collect files to be reverted
|
-- Collect files to be reverted
|
||||||
for i, line in ipairs(luci.util.split(changes)) do
|
for r, tbl in ipairs(changes) do
|
||||||
local r = line:match("^-?([^.]+)")
|
|
||||||
if r then
|
if r then
|
||||||
revert[r] = true
|
revert[r] = true
|
||||||
end
|
end
|
||||||
|
@ -63,5 +85,5 @@ function action_revert()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
luci.template.render("admin_uci/revert", {changes=changes})
|
luci.template.render("admin_uci/revert", {changes=convert_changes(changes)})
|
||||||
end
|
end
|
||||||
|
|
|
@ -120,9 +120,11 @@ end
|
||||||
<%
|
<%
|
||||||
if "admin" == request[1] then
|
if "admin" == request[1] then
|
||||||
require("luci.model.uci")
|
require("luci.model.uci")
|
||||||
local ucic = luci.model.uci.changes()
|
local ucic = 0
|
||||||
if ucic then
|
for n, s in pairs(luci.model.uci.changes()) do
|
||||||
ucic = #luci.util.split(ucic)
|
for no, o in pairs(s) do
|
||||||
|
ucic = ucic + 1;
|
||||||
|
end
|
||||||
end
|
end
|
||||||
%>
|
%>
|
||||||
<div><%:config Konfiguration%>
|
<div><%:config Konfiguration%>
|
||||||
|
@ -138,4 +140,4 @@ end
|
||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
<div id="content">
|
<div id="content">
|
||||||
|
|
|
@ -120,9 +120,11 @@ end
|
||||||
<%
|
<%
|
||||||
if "admin" == request[1] then
|
if "admin" == request[1] then
|
||||||
require("luci.model.uci")
|
require("luci.model.uci")
|
||||||
local ucic = luci.model.uci.changes()
|
local ucic = 0
|
||||||
if ucic then
|
for n, s in pairs(luci.model.uci.changes()) do
|
||||||
ucic = #luci.util.split(ucic)
|
for no, o in pairs(s) do
|
||||||
|
ucic = ucic + 1;
|
||||||
|
end
|
||||||
end
|
end
|
||||||
%>
|
%>
|
||||||
<div><%:config Konfiguration%>
|
<div><%:config Konfiguration%>
|
||||||
|
@ -138,4 +140,4 @@ end
|
||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
<div id="content">
|
<div id="content">
|
||||||
|
|
Loading…
Reference in a new issue