From 31e703920d5c4036a6c893e576b7a773df22511c Mon Sep 17 00:00:00 2001 From: Nick Cabatoff Date: Sun, 8 Oct 2017 17:18:30 -0400 Subject: [PATCH] Report on what changed after a set/match/values. --- src/uci.lua | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/uci.lua b/src/uci.lua index ad348df..96121bd 100644 --- a/src/uci.lua +++ b/src/uci.lua @@ -197,14 +197,31 @@ function set_value(module) local res if nil ~= p["match"] then + local preres = module:ubus_call(conn, "uci", "changes", {config=conf}) + local prechanges = preres["changes"] or {} + local message = { config=conf, values=p["values"], match=p["match"] } - -- TODO: how can we properly report whether a changed happend in this case? - -- Get the entire config before and after to compare? - res = module:ubus_call(conn, "uci", "set", message) + res = module:ubus_call(conn, "uci", "set", message) or {} + + -- Since 'uci changes' returns changes in the order they were made, + -- determine what the 'set' command changed by stripping off the + -- first #prechanges entries from the postchanges. + local postres = module:ubus_call(conn, "uci", "changes", {config=conf}) + local postchanges = postres["changes"] or {} + for i = #prechanges, 1, -1 do + table.remove(postchanges, i) + end + res["changes"] = postchanges + + conn:close() + if #postchanges > 0 then + module:exit_json({msg="Changes made", changed=true, result=res}) + end + module:exit_json({msg="No changes made", changed=false, result=res}) elseif not sec then -- We have to create a section and use "uci add" if not p["type"] then