commit 4f6198094cf4134179d1f9c9fa8f79759a27c87e
[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  = action_revert
8         node("admin", "uci", "apply").target   = action_apply
9 end
10
11 -- This function has a higher priority than the admin_uci/apply template
12 function action_apply()
13         local changes = luci.model.uci.changes()
14         local output  = ""
15         
16         if changes then
17                 local com = {}
18                 local run = {}
19                 
20                 -- Collect files to be applied and commit changes
21                 for i, line in ipairs(luci.util.split(changes)) do
22                         local r = line:match("^-?([^.]+)")
23                         if r then
24                                 com[r] = true
25                                 
26                                 if luci.config.uci_oncommit and luci.config.uci_oncommit[r] then
27                                         run[luci.config.uci_oncommit[r]] = true
28                                 end
29                         end
30                 end
31                 
32                 -- Apply
33                 for config, i in pairs(com) do
34                         luci.model.uci.commit(config)
35                 end 
36                 
37                 -- Search for post-commit commands
38                 for cmd, i in pairs(run) do
39                         output = output .. cmd .. ":" .. luci.sys.exec(cmd) .. "\n"
40                 end
41         end
42         
43         luci.template.render("admin_uci/apply", {changes=changes, output=output})
44 end
45
46
47 function action_revert()
48         local changes = luci.model.uci.changes()
49         if changes then
50                 local revert = {}
51                 
52                 -- Collect files to be reverted
53                 for i, line in ipairs(luci.util.split(changes)) do
54                         local r = line:match("^-?([^.]+)")
55                         if r then
56                                 revert[r] = true
57                         end
58                 end
59                 
60                 -- Revert them
61                 for k, v in pairs(revert) do
62                         luci.model.uci.revert(k)
63                 end
64         end
65         
66         luci.template.render("admin_uci/revert", {changes=changes})
67 end