53b3464f6bbc8f79c38a88d339b1025199192a81
[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 util = require "luci.util"
28         
29         local ret
30         for r, tbl in pairs(changes) do
31                 for s, os in pairs(tbl) do
32                         for o, v in pairs(os) do
33                                 ret = (ret and ret.."\n" or "") .. "%s%s.%s%s%s" % {
34                                         v == "" and "-" or "",
35                                         r,
36                                         s,
37                                         o ~= ".type" and "."..o or "",
38                                         v ~= "" and "="..util.pcdata(v) or ""
39                                 }
40                         end
41                 end
42         end
43         return ret
44 end
45
46 function action_changes()
47         local changes = convert_changes(luci.model.uci.cursor():changes())
48         luci.template.render("admin_uci/changes", {changes=changes})
49 end
50
51 function action_apply()
52         local path = luci.dispatcher.context.path
53         local uci = luci.model.uci.cursor()
54         local changes = uci:changes()
55         local reload = {}
56
57         -- Collect files to be applied and commit changes
58         for r, tbl in pairs(changes) do
59                 table.insert(reload, r)
60                 if path[#path] ~= "apply" then
61                         uci:load(r)
62                         uci:commit(r)
63                         uci:unload(r)
64                 end
65         end
66         
67         local function _reload()
68                 local cmd = uci:apply(reload, true)
69                 return io.popen(cmd)
70         end
71         
72         luci.template.render("admin_uci/apply", {changes=convert_changes(changes), reload=_reload})
73 end
74
75
76 function action_revert()
77         local uci = luci.model.uci.cursor()
78         local changes = uci:changes()
79
80         -- Collect files to be reverted
81         for r, tbl in pairs(changes) do
82                 uci:load(r)
83                 uci:revert(r)
84                 uci:unload(r)
85         end
86         
87         luci.template.render("admin_uci/revert", {changes=convert_changes(changes)})
88 end