Optical improvements
[project/luci.git] / modules / admin-full / luasrc / controller / admin / uci.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14 module("luci.controller.admin.uci", package.seeall)
15
16 function index()
17 local i18n = luci.i18n.translate
18
19 entry({"admin", "uci"}, nil, i18n("config"))
20 entry({"admin", "uci", "changes"}, call("action_changes"), i18n("changes"), 40)
21 entry({"admin", "uci", "revert"}, call("action_revert"), i18n("revert"), 30)
22 entry({"admin", "uci", "apply"}, call("action_apply"), i18n("apply"), 20)
23 entry({"admin", "uci", "saveapply"}, call("action_apply"), i18n("saveapply"), 10)
24 end
25
26 function convert_changes(changes)
27 local ret = {}
28 for r, tbl in pairs(changes) do
29 for s, os in pairs(tbl) do
30 for o, v in pairs(os) do
31 local val, str
32 if (v == "") then
33 str = "-"
34 val = ""
35 else
36 str = ""
37 val = "="..luci.util.pcdata(v)
38 end
39 str = r.."."..s
40 if o ~= ".type" then
41 str = str.."."..o
42 end
43 table.insert(ret, str..val)
44 end
45 end
46 end
47 return table.concat(ret, "\n")
48 end
49
50 function action_changes()
51 local changes = convert_changes(luci.model.uci.cursor():changes())
52 luci.template.render("admin_uci/changes", {changes=changes})
53 end
54
55 function action_apply()
56 local path = luci.dispatcher.context.path
57
58 local output = ""
59 local uci = luci.model.uci.cursor()
60 local changes = uci:changes()
61
62 if changes then
63 local com = {}
64 local run = {}
65
66 -- Collect files to be applied and commit changes
67 for r, tbl in pairs(changes) do
68 if r then
69 if path[#path] ~= "apply" then
70 uci:load(r)
71 uci:commit(r)
72 uci:unload(r)
73 end
74 if luci.config.uci_oncommit and luci.config.uci_oncommit[r] then
75 run[luci.config.uci_oncommit[r]] = true
76 end
77 end
78 end
79
80 -- Search for post-commit commands
81 for cmd, i in pairs(run) do
82 output = output .. cmd .. ":\n" .. luci.util.exec(cmd) .. "\n"
83 end
84 end
85
86
87 luci.template.render("admin_uci/apply", {changes=convert_changes(changes), output=output})
88 end
89
90
91 function action_revert()
92 local uci = luci.model.uci.cursor()
93 local changes = uci:changes()
94 if changes then
95 local revert = {}
96
97 -- Collect files to be reverted
98 for r, tbl in pairs(changes) do
99 uci:load(r)
100 uci:revert(r)
101 uci:unload(r)
102 end
103 end
104
105 luci.template.render("admin_uci/revert", {changes=convert_changes(changes)})
106 end