Skip to content

Commit

Permalink
package: refactor add_to_set/remove_to_set to get rid of last LuCI patch
Browse files Browse the repository at this point in the history
  • Loading branch information
neocturne committed Jan 18, 2017
1 parent a0efa9f commit 04818c1
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

local site = require 'gluon.site_config'
local sysconfig = require 'gluon.sysconfig'
local util = require 'gluon.util'

local ip = require 'luci.ip'
local lutil = require 'luci.util'
Expand Down Expand Up @@ -35,21 +36,24 @@ uci:delete('network', 'client', 'peerdns')
uci:delete('network', 'client', 'sourcefilter')


local ifname = uci:get('network', 'client', 'ifname')
local interfaces = uci:get('network', 'client', 'ifname') or {}

if type(ifname) == 'string' then
uci:delete('network', 'client', 'ifname')
for x in ifname:gmatch("[^%s]+") do
uci:add_to_set('network', 'client', 'ifname', x)
if type(interfaces) == 'string' then
local ifname = interfaces
interfaces = {}
for iface in ifname:gmatch("[^%s]+") do
util.add_to_set(interfaces, iface)
end
end

if sysconfig.lan_ifname and not ifname and not uci:get_bool('network', 'mesh_lan', 'auto') then
for _, lanif in ipairs(lutil.split(sysconfig.lan_ifname, ' ')) do
uci:add_to_set('network', 'client', 'ifname', lanif)
util.add_to_set(interfaces, lanif)
end
end

uci:set_list('network', 'client', 'ifname', interfaces)

uci:save('network')


Expand Down
24 changes: 24 additions & 0 deletions package/gluon-core/luasrc/usr/lib/lua/gluon/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,32 @@ local uci = require('luci.model.uci').cursor()
local lutil = require 'luci.util'
local fs = require 'nixio.fs'


module 'gluon.util'

function add_to_set(t, itm)
for _,v in ipairs(t) do
if v == itm then return false end
end
table.insert(t, itm)
return true
end

function remove_from_set(t, itm)
local i = 1
local changed = false
while i <= #t do
if t[i] == itm then
table.remove(t, i)
changed = true
else
i = i + 1
end
end
return changed
end


function exec(...)
return os.execute(escape_args('', 'exec', ...))
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ $Id$
local uci = luci.model.uci.cursor()
local lutil = require 'luci.util'
local sysconfig = require 'gluon.sysconfig'
local util = require 'gluon.util'

local wan = uci:get_all("network", "wan")
local wan6 = uci:get_all("network", "wan6")
Expand Down Expand Up @@ -127,16 +128,17 @@ function f.handle(self, state, data)
if sysconfig.lan_ifname then
uci:set("network", "mesh_lan", "auto", data.mesh_lan)

local doit
if data.mesh_lan == '1' then
doit = uci.remove_from_set
else
doit = uci.add_to_set
end
local interfaces = uci:get_list("network", "client", "ifname")

for _, lanif in ipairs(lutil.split(sysconfig.lan_ifname, ' ')) do
doit(uci, "network", "client", "ifname", lanif)
if data.mesh_lan == '1' then
util.remove_from_set(interfaces, lanif)
else
util.add_to_set(interfaces, lanif)
end
end

uci:set_list("network", "client", "ifname", interfaces)
end

uci:save("network")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

local sysconfig = require 'gluon.sysconfig'
local site = require 'gluon.site_config'
local util = require 'gluon.util'

local uci = require('luci.model.uci').cursor()

Expand Down Expand Up @@ -34,6 +35,8 @@ uci:section('network', 'interface', 'bat0',
}
)

uci:add_to_set('network', 'client', 'ifname', 'bat0')
local interfaces = uci:get_list('network', 'client', 'ifname')
util.add_to_set(interfaces, 'bat0')
uci:set_list('network', 'client', 'ifname', interfaces)

uci:save('network')

This file was deleted.

0 comments on commit 04818c1

Please sign in to comment.