7443a16b0b750df5a88a962dff93cf58be70bf0c
[project/luci.git] / modules / admin-core / src / controller / admin / uci.lua
1 module("ffluci.controller.admin.uci", package.seeall)
2 require("ffluci.util")
3 require("ffluci.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 = ffluci.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(ffluci.util.split(changes)) do
22                         local r = line:match("^-?([^.]+)")
23                         if r then
24                                 com[r] = true
25                                 
26                                 if ffluci.config.uci_oncommit and ffluci.config.uci_oncommit[r] then
27                                         run[ffluci.config.uci_oncommit[r]] = true
28                                 end
29                         end
30                 end
31                 
32                 -- Apply
33                 for config, i in pairs(com) do
34                         ffluci.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 .. ":" .. ffluci.sys.exec(cmd) .. "\n"
40                 end
41         end
42         
43         ffluci.template.render("admin_uci/apply", {changes=changes, output=output})
44 end
45
46
47 function action_revert()
48         local changes = ffluci.model.uci.changes()
49         if changes then
50                 local revert = {}
51                 
52                 -- Collect files to be reverted
53                 for i, line in ipairs(ffluci.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                         ffluci.model.uci.revert(k)
63                 end
64         end
65         
66         ffluci.template.render("admin_uci/revert", {changes=changes})
67 end