Skip to content

Commit a3b1c1e

Browse files
[qa] Format Lua Files #51
Closes #51
1 parent b55b3e2 commit a3b1c1e

30 files changed

Lines changed: 2327 additions & 2463 deletions

.luacheckrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
files["openwrt-openwisp-monitoring/tests"]={
22
ignore={"Test.*"}
33
}
4+
5+
max_line_length=88

README.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,14 @@ You can inspect the version of openwisp-monitoring currently installed with::
172172

173173
openwisp_monitoring --version
174174

175+
Quality Assurance Checks
176+
------------------------
177+
178+
We use `LuaFormatter <https://luarocks.org/modules/tammela/luaformatter>`_ to format lua files.
179+
Once it is installed, you can format all files by::
180+
181+
./qa-format
182+
175183
Run tests
176184
---------
177185

install-dev.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ git clone https://git.openwrt.org/project/uci.git --depth=1
1515
cd uci && cmake . && make install && cd .. || { echo 'Installing uci failed!' ; exit 1; }
1616
#install nixio
1717
luarocks install https://raw.githubusercontent.com/Neopallium/nixio/master/nixio-scm-0.rockspec
18+
# install luaformatter
19+
git clone --recurse-submodules https://github.com/Koihik/LuaFormatter.git
20+
cd LuaFormatter && cmake . && make install && cd .. || { echo 'Installing LuaFormatter failed'; exit 1; }
1821
# update links to shared libraries
1922
ldconfig -v
2023
# install luaunit
@@ -26,4 +29,4 @@ luarocks install lua-cjson
2629
#install luacov-coveralls
2730
luarocks install luacov-coveralls
2831
#clean
29-
rm -rf json-c libubox uci
32+
rm -rf json-c libubox uci LuaFormatter

openwrt-openwisp-monitoring/files/lib/openwisp/dhcp.lua

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,39 @@
1-
package.path=package.path .. ";../files/lib/?.lua"
1+
package.path = package.path .. ";../files/lib/?.lua"
22

33
-- retrieve dhcp leases
4-
local utils=require('openwisp.monitoring_utils')
5-
local uci=require('uci')
6-
local uci_cursor=uci.cursor()
7-
local io=require('io')
8-
local dhcp={}
4+
local utils = require('openwisp.monitoring_utils')
5+
local uci = require('uci')
6+
local uci_cursor = uci.cursor()
7+
local io = require('io')
8+
local dhcp = {}
99

1010
function dhcp.parse_dhcp_lease_file(path, leases)
11-
local f=io.open(path, 'r')
12-
if not f then
13-
return leases
14-
end
11+
local f = io.open(path, 'r')
12+
if not f then return leases end
1513
for line in f:lines() do
16-
local expiry, mac, ip, name, id=line:match('(%S+)%s+(%S+)%s+(%S+)%s+(%S+)%s+(%S+)')
14+
local expiry, mac, ip, name, id = line:match(
15+
'(%S+)%s+(%S+)%s+(%S+)%s+(%S+)%s+(%S+)')
1716
table.insert(leases, {
18-
expiry=tonumber(expiry),
19-
mac=mac,
20-
ip=ip,
21-
client_name=name,
22-
client_id=id
17+
expiry = tonumber(expiry),
18+
mac = mac,
19+
ip = ip,
20+
client_name = name,
21+
client_id = id
2322
})
2423
end
2524

2625
return leases
2726
end
2827

2928
function dhcp.get_dhcp_leases()
30-
local dhcp_configs=uci_cursor:get_all('dhcp')
31-
local leases={}
29+
local dhcp_configs = uci_cursor:get_all('dhcp')
30+
local leases = {}
3231

33-
if utils.is_table_empty(dhcp_configs) then
34-
return nil
35-
end
32+
if utils.is_table_empty(dhcp_configs) then return nil end
3633

3734
for _, config in pairs(dhcp_configs) do
38-
if config and config['.type']=='dnsmasq' and config.leasefile then
39-
leases=dhcp.parse_dhcp_lease_file(config.leasefile, leases)
35+
if config and config['.type'] == 'dnsmasq' and config.leasefile then
36+
leases = dhcp.parse_dhcp_lease_file(config.leasefile, leases)
4037
end
4138
end
4239
return leases

openwrt-openwisp-monitoring/files/lib/openwisp/interfaces.lua

Lines changed: 86 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,153 +1,149 @@
11
-- retrieve interfaces information
2-
local utils=require('openwisp.monitoring_utils')
2+
local utils = require('openwisp.monitoring_utils')
33

4-
local cjson=require('cjson')
5-
local nixio=require('nixio')
6-
local nixio_data=nixio.getifaddrs()
7-
local io=require('io')
4+
local cjson = require('cjson')
5+
local nixio = require('nixio')
6+
local nixio_data = nixio.getifaddrs()
7+
local io = require('io')
88

9-
local uci=require('uci')
10-
local uci_cursor=uci.cursor()
9+
local uci = require('uci')
10+
local uci_cursor = uci.cursor()
1111

12-
local ubus_lib=require('ubus')
13-
local ubus=ubus_lib.connect()
14-
if not ubus then
15-
error('Failed to connect to ubusd')
16-
end
17-
local interface_data=ubus:call('network.interface', 'dump', {})
12+
local ubus_lib = require('ubus')
13+
local ubus = ubus_lib.connect()
14+
if not ubus then error('Failed to connect to ubusd') end
15+
local interface_data = ubus:call('network.interface', 'dump', {})
1816

19-
local interfaces={}
17+
local interfaces = {}
2018

21-
local specialized_interfaces={
22-
modemmanager=function(_, interface)
23-
local modem=uci_cursor.get('network', interface['interface'], 'device')
24-
local info={}
25-
local general_file=io.popen('mmcli --output-json -m '..modem)
26-
local general=general_file:read("*a")
19+
local specialized_interfaces = {
20+
modemmanager = function(_, interface)
21+
local modem = uci_cursor.get('network', interface['interface'], 'device')
22+
local info = {}
23+
local general_file = io.popen('mmcli --output-json -m ' .. modem)
24+
local general = general_file:read("*a")
2725
general_file:close()
2826
if general and pcall(cjson.decode, general) then
29-
general=cjson.decode(general)
30-
general=general.modem
27+
general = cjson.decode(general)
28+
general = general.modem
3129

3230
if not utils.is_table_empty(general['3gpp']) then
33-
info.imei=general['3gpp'].imei
34-
info.operator_name=general['3gpp']['operator-name']
35-
info.operator_code=general['3gpp']['operator-code']
31+
info.imei = general['3gpp'].imei
32+
info.operator_name = general['3gpp']['operator-name']
33+
info.operator_code = general['3gpp']['operator-code']
3634
end
3735

3836
if not utils.is_table_empty(general.generic) then
39-
info.manufacturer=general.generic.manufacturer
40-
info.model=general.generic.model
41-
info.connection_status=general.generic.state
42-
info.power_status=general.generic['power-state']
37+
info.manufacturer = general.generic.manufacturer
38+
info.model = general.generic.model
39+
info.connection_status = general.generic.state
40+
info.power_status = general.generic['power-state']
4341
end
4442
end
4543

46-
local signal_file=io.popen('mmcli --output-json -m '..modem..' --signal-get')
44+
local signal_file =
45+
io.popen('mmcli --output-json -m ' .. modem .. ' --signal-get')
4746
local signal = signal_file:read("*a")
4847
signal_file:close()
4948
if signal and pcall(cjson.decode, signal) then
50-
signal=cjson.decode(signal)
49+
signal = cjson.decode(signal)
5150
-- only send data if not empty to avoid generating too much traffic
52-
if not utils.is_table_empty(signal.modem) and not utils.is_table_empty(signal.modem.signal) then
51+
if not utils.is_table_empty(signal.modem) and
52+
not utils.is_table_empty(signal.modem.signal) then
5353
-- omit refresh rate
54-
signal.modem.signal.refresh=nil
55-
info.signal={}
54+
signal.modem.signal.refresh = nil
55+
info.signal = {}
5656
-- collect section and values only if not empty
5757
for section_key, section_values in pairs(signal.modem.signal) do
5858
for key, value in pairs(section_values) do
59-
if value ~='--' then
59+
if value ~= '--' then
6060
if utils.is_table_empty(info.signal[section_key]) then
61-
info.signal[section_key]={}
61+
info.signal[section_key] = {}
6262
end
63-
info.signal[section_key][key]=tonumber(value)
63+
info.signal[section_key][key] = tonumber(value)
6464
end
6565
end
6666
end
6767
end
6868
end
6969

70-
return {type='modem-manager', mobile=info}
70+
return {type = 'modem-manager', mobile = info}
7171
end
7272
}
7373

7474
function interfaces.find_default_gateway(routes)
75-
for i=1, #routes do
76-
if routes[i].target=='0.0.0.0' then
77-
return routes[i].nexthop
78-
end
75+
for i = 1, #routes do
76+
if routes[i].target == '0.0.0.0' then return routes[i].nexthop end
7977
end
8078
return nil
8179
end
8280

8381
function interfaces.new_address_array(address, interface, family)
84-
local proto=interface['proto']
85-
if proto=='dhcpv6' then
86-
proto='dhcp'
87-
end
88-
local new_address={
89-
address=address['address'],
90-
mask=address['mask'],
91-
proto=proto,
92-
family=family,
93-
gateway=interfaces.find_default_gateway(interface.route)
82+
local proto = interface['proto']
83+
if proto == 'dhcpv6' then proto = 'dhcp' end
84+
local new_address = {
85+
address = address['address'],
86+
mask = address['mask'],
87+
proto = proto,
88+
family = family,
89+
gateway = interfaces.find_default_gateway(interface.route)
9490
}
9591
return new_address
9692
end
9793

9894
-- collect interface addresses
9995
function interfaces.get_addresses(name)
100-
local addresses={}
101-
local proto=nil
102-
local interface_list=interface_data['interface']
103-
local addresses_list={}
96+
local addresses = {}
97+
local proto = nil
98+
local interface_list = interface_data['interface']
99+
local addresses_list = {}
104100
for _, interface in pairs(interface_list) do
105-
if interface['l3_device']==name then
101+
if interface['l3_device'] == name then
106102
for _, address in pairs(interface['ipv4-address']) do
107103
table.insert(addresses_list, address['address'])
108-
local new_address=interfaces.new_address_array(address, interface, 'ipv4')
104+
local new_address = interfaces.new_address_array(address, interface, 'ipv4')
109105
table.insert(addresses, new_address)
110106
end
111107
for _, address in pairs(interface['ipv6-address']) do
112108
table.insert(addresses_list, address['address'])
113-
local new_address=interfaces.new_address_array(address, interface, 'ipv6')
109+
local new_address = interfaces.new_address_array(address, interface, 'ipv6')
114110
table.insert(addresses, new_address)
115111
end
116112
end
117113
end
118-
for i=1, #nixio_data do
119-
if nixio_data[i].name==name then
114+
for i = 1, #nixio_data do
115+
if nixio_data[i].name == name then
120116
if not utils.is_excluded(name) then
121-
local family=nixio_data[i].family
122-
local addr=nixio_data[i].addr
123-
if family=='inet' then
124-
family='ipv4'
117+
local family = nixio_data[i].family
118+
local addr = nixio_data[i].addr
119+
if family == 'inet' then
120+
family = 'ipv4'
125121
-- Since we don't already know this from the dump, we can
126122
-- consider this dynamically assigned, this is the case for
127123
-- example for OpenVPN interfaces, which get their address
128124
-- from the DHCP server embedded in OpenVPN
129-
proto='dhcp'
130-
elseif family=='inet6' then
131-
family='ipv6'
125+
proto = 'dhcp'
126+
elseif family == 'inet6' then
127+
family = 'ipv6'
132128
if utils.starts_with(addr, 'fe80') then
133-
proto='static'
129+
proto = 'static'
134130
else
135-
local ula=uci_cursor.get('network', 'globals', 'ula_prefix')
136-
local ula_prefix=utils.split(ula, '::')[1]
131+
local ula = uci_cursor.get('network', 'globals', 'ula_prefix')
132+
local ula_prefix = utils.split(ula, '::')[1]
137133
if utils.starts_with(addr, ula_prefix) then
138-
proto='static'
134+
proto = 'static'
139135
else
140-
proto='dhcp'
136+
proto = 'dhcp'
141137
end
142138
end
143139
end
144-
if family=='ipv4' or family=='ipv6' then
140+
if family == 'ipv4' or family == 'ipv6' then
145141
if not utils.has_value(addresses_list, addr) then
146142
table.insert(addresses, {
147-
address=addr,
148-
mask=nixio_data[i].prefix,
149-
proto=proto,
150-
family=family
143+
address = addr,
144+
mask = nixio_data[i].prefix,
145+
proto = proto,
146+
family = family
151147
})
152148
end
153149
end
@@ -158,25 +154,22 @@ function interfaces.get_addresses(name)
158154
end
159155

160156
function interfaces.get_interface_info(name, netjson_interface)
161-
local info={
162-
dns_search=nil,
163-
dns_servers=nil
164-
}
157+
local info = {dns_search = nil, dns_servers = nil}
165158
for _, interface in pairs(interface_data['interface']) do
166-
if interface['l3_device']==name then
159+
if interface['l3_device'] == name then
167160
if next(interface['dns-search']) then
168-
info.dns_search=interface['dns-search']
161+
info.dns_search = interface['dns-search']
169162
end
170163
if next(interface['dns-server']) then
171-
info.dns_servers=interface['dns-server']
164+
info.dns_servers = interface['dns-server']
172165
end
173-
if netjson_interface.type=='bridge' then
174-
info.stp=uci_cursor.get('network', interface['interface'], 'stp')=='1'
166+
if netjson_interface.type == 'bridge' then
167+
info.stp = uci_cursor.get('network', interface['interface'], 'stp') == '1'
175168
end
176169
-- collect specialized info if available
177-
local specialized_info=specialized_interfaces[interface.proto]
170+
local specialized_info = specialized_interfaces[interface.proto]
178171
if specialized_info then
179-
info.specialized=specialized_info(name, interface)
172+
info.specialized = specialized_info(name, interface)
180173
end
181174
end
182175
end
@@ -185,17 +178,13 @@ end
185178

186179
function interfaces.get_vpn_interfaces()
187180
-- only openvpn supported for now
188-
local items=uci_cursor:get_all('openvpn')
189-
local vpn_interfaces={}
181+
local items = uci_cursor:get_all('openvpn')
182+
local vpn_interfaces = {}
190183

191-
if utils.is_table_empty(items) then
192-
return {}
193-
end
184+
if utils.is_table_empty(items) then return {} end
194185

195186
for _, config in pairs(items) do
196-
if config and config.dev then
197-
vpn_interfaces[config.dev]=true
198-
end
187+
if config and config.dev then vpn_interfaces[config.dev] = true end
199188
end
200189
return vpn_interfaces
201190
end

0 commit comments

Comments
 (0)