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