make use of the new features in the binding for uci v0.4.0 - fixes remaining dependen...
[project/luci.git] / modules / admin-core / luasrc / controller / admin / uci.lua
1 module("luci.controller.admin.uci", package.seeall)
2 require("luci.util")
3 require("luci.sys")
4
5 function index()
6         node("admin", "uci", "changes").target = template("admin_uci/changes")
7         node("admin", "uci", "revert").target  = call("action_revert")
8         node("admin", "uci", "apply").target   = call("action_apply")
9 end
10
11 function convert_changes(changes)
12         local ret = {}
13         for r, tbl in pairs(changes) do
14                 for s, os in pairs(tbl) do
15                         for o, v in pairs(os) do
16                                 local val, str
17                                 if (v == "") then
18                                         str = "-"
19                                         val = ""
20                                 else
21                                         str = ""
22                                         val = "="..v
23                                 end
24                                 str = str.."."..r
25                                 if o ~= ".type" then
26                                         str = str.."."..o
27                                 end
28                                 table.insert(ret, str..val)
29                         end
30                 end
31         end
32 end
33
34 -- This function has a higher priority than the admin_uci/apply template
35 function action_apply()
36         local changes = luci.model.uci.changes()
37         local output  = ""
38         
39         if changes then
40                 local com = {}
41                 local run = {}
42                 
43                 -- Collect files to be applied and commit changes
44                 for r, tbl in pairs(changes) do
45                         if r then
46                                 com[r] = true
47                                 
48                                 if luci.config.uci_oncommit and luci.config.uci_oncommit[r] then
49                                         run[luci.config.uci_oncommit[r]] = true
50                                 end
51                         end
52                 end
53                 
54                 -- Apply
55                 for config, i in pairs(com) do
56                         luci.model.uci.commit(config)
57                 end 
58                 
59                 -- Search for post-commit commands
60                 for cmd, i in pairs(run) do
61                         output = output .. cmd .. ":" .. luci.sys.exec(cmd) .. "\n"
62                 end
63         end
64         
65         
66         luci.template.render("admin_uci/apply", {changes=convert_changes(changes), output=output})
67 end
68
69
70 function action_revert()
71         local changes = luci.model.uci.changes()
72         if changes then
73                 local revert = {}
74                 
75                 -- Collect files to be reverted
76                 for r, tbl in ipairs(changes) do
77                         if r then
78                                 revert[r] = true
79                         end
80                 end
81                 
82                 -- Revert them
83                 for k, v in pairs(revert) do
84                         luci.model.uci.revert(k)
85                 end
86         end
87         
88         luci.template.render("admin_uci/revert", {changes=convert_changes(changes)})
89 end